简体   繁体   English

Php重定向不起作用

[英]Php redirect doesn't work

I'm doing a project in php mvc but without framework. 我在php mvc中做了一个项目,但是没有框架。 I organized the files in this way: 我用这种方式组织了文件:

  • Directory public: there are files accessible from the outside 目录公共:有外部可访问的文件
  • Directory View: there are files .php but essentially only html 目录视图:有文件.php,但基本上只有html
  • Direcory Model: contains the files related to the database connection, and classes that define the objects, for example, the class User.php Direcory Model:包含与数据库连接相关的文件,以及定义对象的类,例如User.php类
  • Directory Controller: contains a 'generic' class Controller and Controller classes more 'specific' 目录控制器:包含'通用'类控制器和控制器类更“特定”

I creat a login page (View/login.php) with a form whose action value is public/index.php. 我创建了一个登录页面(View / login.php),其中包含一个动作值为public / index.php的表单。 In index.php there is this code: 在index.php中有这样的代码:

$controller = new LoginController();
$view = $controller->invoke();
$view->render();

invoke() is a function in Controller/LoginController.php that reads the data entered by the user and controls them, if they are correct (there is user in the database with the username and password) then creates two global variables and make a redirect: invoke()是Controller / LoginController.php中的一个函数,它读取用户输入的数据并控制它们,如果它们是正确的(数据库中有用户使用用户名和密码)则创建两个全局变量并进行重定向:

 $_SESSION['logged_in'] = 1;
 $_SESSION['username'] = $username;
 $url = "../public/home.php";
 header("Location: $url", true, 302);
 exit();

public/home.php does this: public / home.php这样做:

$controller = new HomeController();
$view = $controller->invoke();
$view->render();

HomeController is a class that extends Controller. HomeController是一个扩展Controller的类。 The constructor of Controller see if there are the variables $_SESSION['logged_in'] and $_SESSION['username']. Controller的构造函数查看是否存在变量$ _SESSION ['logged_in']和$ _SESSION ['username']。 If they not exists it makes a redirect to public/index.php. 如果它们不存在,则会重定向到public / index.php。

My problem is that the row with header("refresh:0; url=../public/home.php "); 我的问题是带标题的行(“refresh:0; url = .. / public / home.php”); does not redirect. 不重定向。

Explain: when I insert the correct data (registered user) redirects for a short time but then returns to home.php. 说明:当我插入正确的数据(注册用户)重定向一小段时间,然后返回home.php。 Instead it should redirect to home.php and stay there, do not go to index.php . 相反,它应该重定向到home.php并留在那里,不要去index.php。

I also tried with header("refresh: 0; url = ../public/home.php"); 我也试过用header("refresh: 0; url = ../public/home.php"); but it's the same problem. 但这是同样的问题。

How can I solve this problem? 我怎么解决这个问题? Thank you and sorry for my poor English! 谢谢你,对不起我的英语不好!

Add php ob_start() function on top of the page. 在页面顶部添加php ob_start()函数。 If you call ob_start() while another ob_start() is active. 如果在另一个ob_start()处于活动状态时调用ob_start()。 Just make sure that you call ob_end_flush() the appropriate number of times. 只需确保调用ob_end_flush()适当的次数。 If multiple output callback functions are active, output is being filtered sequentially through each of them in nesting order. 如果多个输出回调函数处于活动状态,则将按嵌套顺序依次对每个输出回调函数进行过滤。

Ref: http://in2.php.net/ob_start 参考: http//in2.php.net/ob_start

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM