简体   繁体   English

使用会话脚本重定向站点

[英]Redirect site using session script

I´m trying to redirect visitor when visits the page for first time to site 1. The second visit the script will redirect the visitor to site 2. I want to use session.我试图在第一次访问页面时将访问者重定向到站点 1。第二次访问脚本会将访问者重定向到站点 2。我想使用会话。 But it doesn´t work right and I dont know where is the mistake.但它不起作用,我不知道错误在哪里。

Logic should be: If you are here for the first time you will be redirected to site 1, if you are here for the second time you will be redirected to side 2.逻辑应该是:如果您第一次来到这里,您将被重定向到站点 1,如果您第二次来到这里,您将被重定向到站点 2。

this is the code i made:这是我制作的代码:

session_start();

if ($_SESSION["header"] = " " || !isset($_SESSION)) 
{
    $_SESSION["header"] = "1"; 
    echo header("Location: http://site 1");

    }
else
{
  session_destroy();
    echo header("Location: http://site 2");
    exit();
}

well I´m not sure if the session is the right way how to do it好吧,我不确定会议是否是正确的方法

Thanks a lot.非常感谢。

It's not possible and not recommended to do it with a session.这是不可能的,也不建议通过会话来完成。 Because after a while the session gets destroyed automatically.因为一段时间后会话会自动销毁。 I rather you to use cookies.我宁愿你使用cookies。 The code is similar to your code.该代码类似于您的代码。 The only difference between sessions and cookies is that cookies can be setted for lifetime会话和 cookie 的唯一区别是 cookie 可以设置为终身

You're using this code which is wrong.您正在使用此代码是错误的。

$_SESSION["header"] = " "

$_SESSION["header"] = " " means to assign ' ' to $_SESSION["header"] $_SESSION["header"] = " "表示将 ' ' 分配给$_SESSION["header"]


It should be:它应该是:

$_SESSION["header"] == " "

$_SESSION["header"] == " " means $_SESSION["header"] is equal to ' ' $_SESSION["header"] == " "表示$_SESSION["header"]等于 ' '


== is for comparison , = is for assignment , and === is for identical or same type . ==用于比较=用于赋值,而===用于相同相同类型

More information at http://php.net/manual/en/language.operators.comparison.php .更多信息请访问http://php.net/manual/en/language.operators.comparison.php

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

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