gpt4 book ai didi

javascript - php中如何制作登录后欢迎信息的转场效果?

转载 作者:行者123 更新时间:2023-11-30 19:04:35 25 4
gpt4 key购买 nike

我有这段代码可以在网站上设置 cookie:

if (isset($_COOKIE[$_first_name.$_last_name])){
echo '<html><head></head><body></br></br></br><h1><center>Hello, '.$_COOKIE[$_first_name.$_last_name].'! Welcome back!</center></h1></body></html>';
sleep(2);
header('Location: ../index.php');
}
else if (setcookie( $_first_name.$_last_name, $_first_name.' '.$_last_name, time() + 60*60*24*5))
;

第一次它会注册名称,第二次,它不会给我欢迎消息,而是会立即将我引导至主页。我希望它等待 2 秒让用户看到欢迎消息,然后自动将用户定向到主页。

更多信息

<?php
//error_reporting(E_ERROR);
session_start();
include('loginScript.php'); // Includes Login Script

if(isset($_SESSION['login_user'])){
}
?>
<html>
...
...
</html>

在上面的代码中,我调用了包含检查 cookie 的代码的 loginScript.php。

loginScript.php 文件中的部分

  if ($rows == 1) {
$result = mysqli_fetch_array($query);

$_SESSION['login_user']=$username; // Initializing Session
// $_SESSION['items'][] =$username;
$_SESSION['firstname'] =$result['Firstname'];
$_first_name = $_SESSION['firstname'];
$_SESSION['lastname'] =$result['Lastname'];
$_last_name = $_SESSION['lastname'];

if (isset($_COOKIE[$_first_name.$_last_name])){
echo '<html><head></head><body></br></br></br><h1><center>Hello,
'.$_COOKIE[$_first_name.$_last_name].'! Welcome back!</center></h1></body>
</html>';
ob_flush();
flush();
//sleep(2);
echo "<script>setTimeout(()=>
{document.location.href='index.php'},2000);</script>";
}
else if (setcookie( $_first_name.$_last_name, $_first_name.'
'.$_last_name, time() + 60*60*24*5)){
// Code of rest of the entire login page
}
}
else {
$error = "Username or Password is invalid";
}
mysqli_close($connection); // Closing Connection

最佳答案

首先,您不能sleep 输出而不在php 中刷新。你将不得不打电话

ob_flush();
flush();

为了强制 php 在休眠和执行其余代码之前输出内容。

其次,您不能在更改 header 之前输出任何内容 - 在您的情况下,设置位置 header 。一个简单的解决方法是改用 javascript 重定向页面。所以你会得到这样的东西:

if (isset($_COOKIE[$_first_name.$_last_name])){
echo '<html><head></head><body></br></br></br><h1><center>Hello, '.$_COOKIE[$_first_name.$_last_name].'! Welcome back!</center></h1></body></html>';
ob_flush();
flush();
sleep(2);
echo "<script>document.location.href='index.php'</script>";
die(); //added to prevent the rest of the output
}
else if (setcookie( $_first_name.$_last_name, $_first_name.' '.$_last_name, time() + 60*60*24*5))
;

但是,除非绝对必要,否则通常不建议使用 sleep,因为 sleep 会占用您的服务器资源,想象一下如果每个人都在 sleep ing,这会浪费您的资源并阻止服务器处理新请求。

我们可以通过使用 javascript settimeout 将此延迟卸载到客户端来进一步解决此问题。

if (isset($_COOKIE[$_first_name.$_last_name])){
echo '<html><head></head><body></br></br></br><h1><center>Hello, '.$_COOKIE[$_first_name.$_last_name].'! Welcome back!</center></h1></body></html>';
ob_flush();
flush();
//sleep(2);
echo "<script>setTimeout(()=>{document.location.href='index.php'},2000);</script>";
}
else if (setcookie( $_first_name.$_last_name, $_first_name.' '.$_last_name, time() + 60*60*24*5)){
// Code of rest of the entire login page
}

最后但同样重要的是,正如 Richard 在评论中提到的,从客户端处理这一切实际上是一种更好的做法,这样您还可以更好地控制转换等。这超出了范围这个问题,但你可以考虑按照他的建议重新设计程序流程。否则,上述解决方法应该可以满足您的需求。

关于javascript - php中如何制作登录后欢迎信息的转场效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59119934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com