简体   繁体   English

PHP-成功登录后,执行javascript并重定向到新页面

[英]PHP - Execute javascript and redirect to a new page after successful login

I have this login.php and a javascript to sync the date and time from the client browser to the server of my openwrt router. 我有这个login.php和一个javascript,用于将日期和时间从客户端浏览器同步到我的openwrt路由器的服务器。 What I want is, every time a user successfully logs in, it'll execute that javascript and then redirect to status.php which is the home page. 我想要的是,每次用户成功登录,它将执行该javascript,然后重定向到主页的status.php Here's what I've tried so far 到目前为止,这是我尝试过的

if ($_POST['username'] == $user[0] && $_POST['password'] == $pass[0]) {
        $_SESSION['loggedin'] = 1;
        echo '<script src="date.js"></script>';
        header("Location: status.php");
        exit;
    }

But it gives me this error: 但这给了我这个错误:

Warning: Cannot modify header information - headers already sent by (output started at /www/login.php:10) in /www/login.php on line 11 警告:无法修改标头信息-第11行的/www/login.php中已经发送过的标头(输出始于/www/login.php:10)

How do I fix it ? 我如何解决它 ?

Some of you may think my question is a duplicate but please understand that I need to execute that javascript after successful login. 你们中有些人可能认为我的问题是重复的,但请理解,成功登录后,我需要执行该javascript。

The problem is that you cannot send the header after echoing. 问题是回显后无法发送标头。 To solve this, ob_start is used to turn output buffering on which means when using echo , no output is sent but instead it is stored in an internal buffer and when we call ob_end_flush after we have called header() , the internal buffer contents will be flushed and output buffering will stop. 为了解决这个问题, ob_start用于打开输出缓冲,这意味着在使用echo ,不发送任何输出,而是将其存储在内部缓冲区中,当我们在调用header()之后调用ob_end_flush时,内部缓冲区的内容将为刷新,输出缓冲将停止。

Try: 尝试:

ob_start();
echo '<script src="date.js"></script>';
header("Refresh: 5; url=status.php");
ob_end_flush();

Hope this helps. 希望这可以帮助。

you can't put header.. after "echo .." or other html element .. 您不能在“ echo ..”或其他html元素..之后放置header ..

why you need to put your date.js ? 为什么需要放置date.js?

if ($_POST['username'] == $user[0] && $_POST['password'] == $pass[0]) {
    $_SESSION['loggedin'] = 1;
 //   echo '<script src="date.js"></script>';
    header("Location: status.php");
    exit;
}

将您的JavaScript代码放在您的php代码之外,打开您的php代码,然后在您的JavaScript代码关闭该php之前,将您的JavaScript代码正常放置而不会产生回声,然后再将php的begin和end标记放入

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

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