Php 注销时重定向并显示“您已成功注销!”
我的网站上有会员服务。当前,当有人注销时,他们会被重定向到logout.php,其中包含以下代码:Php 注销时重定向并显示“您已成功注销!”,php,html,Php,Html,我的网站上有会员服务。当前,当有人注销时,他们会被重定向到logout.php,其中包含以下代码: <?php //check if the login session does no exist if(strcmp($_SESSION['uid'],”) == 0){ //if it doesn't display an error message echo "<center>You need
<?php
//check if the login session does no exist
if(strcmp($_SESSION['uid'],”) == 0){
//if it doesn't display an error message
echo "<center>You need to be logged in to log out!</center>";
}else{
//if it does continue checking
//update to set this users online field to the current time
mysql_query("UPDATE `users` SET `online` = '".date('U')."' WHERE `id` = '".$_SESSION['uid']."'");
//destroy all sessions canceling the login session
session_destroy();
//display success message
echo "<center>You have successfully logged out!<br><a href = '/review-pratt/index.php' class='icon-button star'>Return Home</button></center>";
}
?>
有很多解决方案,但几乎所有的解决方案都需要logout.php来传递消息,而index.php则需要代码来显示消息 我首选的方法是将消息作为URL参数传递。用于重新定向,用于缩短url中的文本,并确保url不会被废弃
//Redirect with success message
header('Location: /index.php?msg=' . urlencode(base64_encode("You have been successfully logged out!")));
然后,在index.php页面上
if ($_GET['msg'])
{
echo '<div class="success_message">' . base64_decode(urldecode($_GET['msg'])) . '</div>';
}
编辑:如果您的标题已经发送出去了,您是否在这些标题上方的一行上回显了一些文本?您可以使用Javascript进行重定向
将标题“位置:”替换为:echo 有很多解决方案,但几乎所有的解决方案都需要logout.php来传递消息,而index.php则需要代码来显示消息 我首选的方法是将消息作为URL参数传递。用于重新定向,用于缩短url中的文本,并确保url不会被废弃
//Redirect with success message
header('Location: /index.php?msg=' . urlencode(base64_encode("You have been successfully logged out!")));
然后,在index.php页面上
if ($_GET['msg'])
{
echo '<div class="success_message">' . base64_decode(urldecode($_GET['msg'])) . '</div>';
}
编辑:如果您的标题已经发送出去了,您是否在这些标题上方的一行上回显了一些文本?您可以使用Javascript进行重定向
将标题“位置:”替换为:echo 您可以这样做:
header('location: index.php?status=loggedout');
<?php
if(!empty($_GET['status'])){
echo '<div>You have been logged out!</div>';
}
?>
在index.php文件中,只需查看status是否为空,并显示一个状态如下的div:
header('location: index.php?status=loggedout');
<?php
if(!empty($_GET['status'])){
echo '<div>You have been logged out!</div>';
}
?>
在if语句中,您还可以清除用户会话。您可以执行以下操作:
header('location: index.php?status=loggedout');
<?php
if(!empty($_GET['status'])){
echo '<div>You have been logged out!</div>';
}
?>
在index.php文件中,只需查看status是否为空,并显示一个状态如下的div:
header('location: index.php?status=loggedout');
<?php
if(!empty($_GET['status'])){
echo '<div>You have been logged out!</div>';
}
?>
在if语句中,您还可以清除用户会话。您可以使用Noty插件在web应用程序上启用通知。 请看这里: 实现应该如下所示: 将用户重定向到index.php?注销=1 使用查询字符串参数填充隐藏字段。 使用noty可在页面加载时显示隐藏字段值。 下面是一个代码示例:
<?php
if(!empty($_GET['logout'])){
echo '<input id="logoutMsg" value="You have been logged out!" />';
}
?>
<script>
var logoutMsg = $('#logoutMsg').val();
var noty = noty({text: logoutMsg });
</script>
您可以使用Noty插件在web应用程序上启用通知。 请看这里: 实现应该如下所示: 将用户重定向到index.php?注销=1 使用查询字符串参数填充隐藏字段。 使用noty可在页面加载时显示隐藏字段值。 下面是一个代码示例:
<?php
if(!empty($_GET['logout'])){
echo '<input id="logoutMsg" value="You have been logged out!" />';
}
?>
<script>
var logoutMsg = $('#logoutMsg').val();
var noty = noty({text: logoutMsg });
</script>
如果要在成功消息之后立即重定向,请使用以下代码:-
<?php
//check if the login session does no exist
if(strcmp($_SESSION['uid'],”) == 0){
//if it doesn't display an error message
echo "<center>You need to be logged in to log out!</center>";
}else{
//if it does continue checking
//update to set this users online field to the current time
mysql_query("UPDATE `users` SET `online` = '".date('U')."' WHERE `id` = '".$_SESSION['uid']."'");
//destroy all sessions canceling the login session
session_destroy();
//display success message
echo "<center>You have successfully logged out!
echo '<meta http-equiv="Refresh" content="0;url=http://url.which.you.want.to.be.redirected.to">';
}
}
?>
如果要在成功消息之后立即重定向,请使用以下代码:-
<?php
//check if the login session does no exist
if(strcmp($_SESSION['uid'],”) == 0){
//if it doesn't display an error message
echo "<center>You need to be logged in to log out!</center>";
}else{
//if it does continue checking
//update to set this users online field to the current time
mysql_query("UPDATE `users` SET `online` = '".date('U')."' WHERE `id` = '".$_SESSION['uid']."'");
//destroy all sessions canceling the login session
session_destroy();
//display success message
echo "<center>You have successfully logged out!
echo '<meta http-equiv="Refresh" content="0;url=http://url.which.you.want.to.be.redirected.to">';
}
}
?>
另外,出于安全原因,请确保在将变量插入数据库时转义变量。查找SQL注入,mysql\u real\u escape\u string在顶部添加以下内容:为了安全起见,请确保在将变量插入数据库时正在转义变量。查找SQL注入,mysql_real_escape_string在顶部添加以下内容:Ok,这可以重定向到index.php,但是您如何传递消息?@user2109152这应该会呼应您的成功消息。您需要使用CSS来设置它的样式,并使它在顶部形成横幅。如果您不确定如何执行此操作,我会提出一个新问题。您的编辑没有显示url中包含的消息,我尝试像您在第一个url中那样添加它,但它只给了我一个错误。@user2109152对此表示抱歉。尝试回声;如果用户刷新页面怎么办?消息仍将显示…确定重定向到index.php,但您如何传递消息?@user2109152这将呼应您的成功消息。您需要使用CSS来设置它的样式,并使它在顶部形成横幅。如果您不确定如何执行此操作,我会提出一个新问题。您的编辑没有显示url中包含的消息,我尝试像您在第一个url中那样添加它,但它只给了我一个错误。@user2109152对此表示抱歉。尝试回声;如果用户刷新页面怎么办?消息仍将显示…@用户会话将在注销页面的标题前清除,然后在if语句中如何清除。@此方法由$\u GET url变量决定,如果我们希望按会话进行清除,那么将如何执行。@MohitKumar如果您已经在处理会话,我建议您另外问一个问题。我知道我只是不想破坏原来的问答@user会话,在注销页面的标题之前,它会被清除,那么我们如何在if语句中清除。@这个方法是通过$\u GET url变量实现的,如果我们希望通过会话来实现,那么它将如何实现。@MohitKumar如果您已经在处理会话,我建议您单独问一个问题。我知道我不想破坏原来的问题/答案