PHP Session 用于计算访问次数和重定向 if (session['visits']=1)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11481806/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:37:00  来源:igfitidea点击:

PHP Session for counting visits and redirect if (session['visits']=1)

phpsessionredirect

提问by Leo

I am trying to make a page on which if you user comes first time it redirects to the index page but if user comes 2nd time the page doesn't redirect. I am using simple php session for counting the visit and an if statement for checking condition:

我正在尝试创建一个页面,如果您第一次访问该页面,它会重定向到索引页面,但如果用户第二次访问该页面,则该页面不会重定向。我正在使用简单的 php 会话来计算访问次数,并使用 if 语句来检查条件:

<?php
session_start(); 
$_SESSION['views'] = $SESSION['views']+1;
if($SESSION['views'] = 1){
header("location:index.php");
}
?>

The problem is to initialize the array with zero i.e

问题是用零初始化数组,即

<?php    
$_SESSION['views']=0;
?>

It's niether as much simple as it seems and nor much tough.

它既没有看起来那么简单,也没有那么难。

回答by johngirvin

Use isset()to check if the key has been created:

使用isset()检查重点已创建:

<?php
session_start();

if (!isset($_SESSION['views'])) { 
    $_SESSION['views'] = 0;
}

$_SESSION['views'] = $_SESSION['views']+1;

if ($_SESSION['views'] == 1) {
    header("location:index.php");
}
?>

Also be careful: you had if ($SESSION['views'] = 1)which is sets the key to 1 not compares it, and the correct superglobal name is $_SESSIONnot $SESSION.

还要小心:你有if ($SESSION['views'] = 1)which is 将键设置为 1 不比较它,正确的超全局名称$_SESSION不是$SESSION

回答by ryanc1256

first of all (where @nivrig and @Yan dont fix)

首先(@nivrig 和 @Yan 没有修复)

if($_SESSION['views'] = 1){
header("location:index.php");
}

should be

应该

if ($_SESSION['views'] == 1){ 
header("location:index.php");
}

and go with @nivrig his example is right

和@nivrig一起去,他的例子是对的

回答by RobB

Since you do not want to worry about initializing the variable first, utilize isset()which will check if the variable is set and is not NULL:

由于您不想首先担心初始化变量,因此请使用isset()它将检查变量是否已设置且不为 NULL:

<?php

session_start(); 

if(isset($_SESSION['views'])){
    $_SESSION['views']++;
}else{
    $_SESSION['views'] = 0;
    header("location: index.php");
    exit();
}
?>

If you do not care about how many times the user has been to the page, then you could simply use:

如果您不关心用户访问该页面的次数,那么您可以简单地使用:

<?php

session_start(); 

if(!isset($_SESSION['views'])){
    $_SESSION['views'] = 0;
    header("location: index.php");
    exit();
}
?>

The second solution does not worry about increasing the number of viewsand just checks if the value has been set yet or not. If it hasn't been set, then you can assert that this is the user's first time visiting the page and you would then set the variable and redirect the user. By setting the variable, you will ensure that the user does not enter a loop after they have already viewed the page. This approach should better simplify things as initially requested.

第二种解决方案不担心增加数量,views只检查值是否已设置。如果尚未设置,则您可以断言这是用户第一次访问该页面,然后您将设置变量并重定向用户。通过设置变量,您将确保用户在查看页面后不会进入循环。这种方法应该按照最初的要求更好地简化事情。

Also make sure you put exit()after redirecting the user. This makes sure that any code below does not get executed when redirecting the user.

还要确保exit()在重定向用户之后放置。这可确保在重定向用户时不会执行以下任何代码。

回答by Horen

Agree with nivrig. Check if the session has been set.

同意 nivrig。检查会话是否已设置。

You have a couple of syntax errors in your code though:

但是,您的代码中有几个语法错误:

Make sure you write $_SESSION and not $SESSION and when you use an if case you have to use == to compare. A single = will assign a value to a variable.

确保你写的是 $_SESSION 而不是 $SESSION 并且当你使用 if case 你必须使用 == 来比较。单个 = 将为变量赋值。