使用cookie的PHP / Javascript循环 - 重定向

时间:2013-01-13 02:49:51

标签: php javascript cookies

所以我对整个Javascript / PHP事情有点新意。用这两种语言编码确实非常有趣。现在,如果用户访问chrisrjones.com,他们将被重定向到chrisrjones.com/splash.php。有两个单选按钮询问用户是否喜欢启动页面。根据用户的选择,cookie值设置为true或false。另外,一旦用户点击“输入”按钮,就会创建一个名为“访问”的cookie。

如果用户选择“是”,则启动页面是愚蠢的,他们会进入该站点,但如果在浏览器中关闭了窗口/选项卡,则会在重新加载站点时显示启动页面。

如果用户选择“否”,那么初始页面就是愚蠢的,他们根本无法进入该网站。

点击任一按钮后,有没有办法让用户进入网站? (必须有办法)

还有一种方法可以记住用户是否选择了“是”而不是重定向它们。

index.php 文件如下所示

<?php
$cookie_splash = $_COOKIE['nosplash'];
$cookie_visit = $_COOKIE['visit'];

if ($cookie_splash == "false" || $cookie_splash == "" && $cookie_visit == "") {
    echo "<script type = text/javascript>";
    echo "window.location = 'http://chrisrjones.com/splash.php'";
    echo "</script>";
}

if ($cookie_splash == "false" && $cookie_visit == "true") {
    echo "<script type = text/javascript>";
    echo "window.location = 'http://chrisrjones.com/index.php'";
    echo "</script>";

}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>chrisRjones.com</title>
<!-- default - stylesheets at the bottom override styles at the top -->
<link rel="stylesheet" type="text/css" media="screen" href="stylesheet.css" />

<!-- favicon -->
<link href="favicon.ico" rel="icon" type="image/x-icon" />

</head>

<body>
<!-- cut for readablitiy -->
</body>
</html>

splash.php

<!DOCTYPE html>

<html lang="en">

<head>
<title>chrisRjones.com - Splash</title>

<script type="text/javascript">

function valForm() {

    if(!document.getElementById('splash_false').checked && !document.getElementById('splash_true').checked  ) {
        alert('You either like splash pages or you don\'t choose one ...please');
        return false;
    }
}

</script>

<!-- Javascript radiobutton form validation END -->


</head>

<body>
<img src="<?php echo $path . $img ?>" alt="" width="640" height="auto"  />
<h1>Refresh the page to enjoy more pictures.</h1><br />

<p>
<form name="tosplashornottosplash" action="splash-process.php" method="post" onSubmit="return valForm()">
Splash pages are stupid.

<input type="radio" name="splash" id="splash_false" value="false" /> No
<input type="radio" name="splash" id="splash_true" value="true" /> Yes

<input type="submit" name="splashSubmit" onClick="return valForm(tosplashornottosplash)" value="Enter" />
</form>
</p>



</body>
</html>

防溅process.php

<?php 

setcookie("visit", "true"); // delete cookie when browser / tab / session is clossed

$splashvar = $_POST["splash"];

if ( $splashvar == "false" ) {
    // create cookie - nosplash 1
    setcookie("nosplash", "false", time()+3600); // expires in one hour
}
    else {
        // create cookie - nosplash 0
        setcookie("nosplash", "true", time()+3600); // expires in one hour
    }

echo "<script type = text/javascript>";
echo "window.location = 'http://chrisrjones.com/index.php'";
echo "</script>";

?>

<!DOCTYPE html>

<html lang="en">

<head>
<title>chrisRjones.com - splash-process</title>

</head>

<body>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

如果在Index.php中,您可以尝试删除第二个。它并没有真正做任何事情,因为用户已经在那里。