php 如果会话不存在则重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19403687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
redirect if session doen't exist
提问by user2881430
i m trying to redirect to attempt page if user fills incorrect information... on the other hand if attempt page got refreshed want it to be redirected on login page... i m using session for that...
如果用户填写不正确的信息,我试图重定向到尝试页面......另一方面,如果尝试页面被刷新,希望它在登录页面上重定向......我正在使用会话......
if (isset($c))
{
$q = mysql_query("select * from registeration where email='$a' and password='$b'");
$r = mysql_num_rows($q);
if($r)
{
$_SESSION["Authenticated"] = 1;
$_SESSION['id'] = $a;
}
else
{
$_SESSION["Authenticated"] = 0;
$_SESSION["att"] = 1;
}
if(isset($_SESSION["att"]))
{
header("location:attempt1.php");
}
else
{
session_write_close();
header('location:profile.php');
}
}
the above mentioned code is redirecting on attempt1.php but code on attempt1.php redirecting back to index.php
上面提到的代码在尝试1.php 上重定向,但在尝试1.php 上的代码重定向回index.php
session_start();
if (!isset($_SESSION["att"]))
{
echo "<meta http-equiv=\"refresh\" content=\"0; url=index.php\">";
}
i want attempt1.php code to redirect on user on index.php if session is not set.. and destroy session on refresh or direct page load... so that direct access to this page results in redirection to index page please help friends..
如果会话未设置,我希望尝试 1.php 代码在 index.php 上重定向用户......并在刷新或直接页面加载时销毁会话......以便直接访问此页面会导致重定向到索引页面,请帮助朋友。 .
ANSWERANSWERANSWERall the questions i asked i made silly mistakes... here in this question i had not started session wile storing session variables.... add the below code in first line of login script
回答回答回答我问的所有问题,我犯了一些愚蠢的错误……在这个问题中,我还没有开始会话并存储会话变量……在登录脚本的第一行添加以下代码
session_start();
回答by Prafulla
//try this code
<?php
session_start();
if (!isset($_SESSION["att"]))
{
header("location: index.php");
}
?>
回答by Matthew Riches
I think your asking to redirect if a session doesn't exist, since a session requires an ID you should be able to check against that:
我认为如果会话不存在,您要求重定向,因为会话需要一个 ID,您应该能够检查它:
if(!(session_id()) header("Location: /mypage.php");
回答by Anand Solanki
Try this:
尝试这个:
if(empty($_SESSION))
{
header("Location: /mypage.php");
}
else
{
// do something ....
}
- Thanks
- 谢谢