[英]PHP Function - Cannot unset $_SESSION variable
I cannot unset a session variable for a PHP Function, 我无法为PHP函数设置会话变量,
From the code below, If I enter a review into a site, with the $_SESSION['KEYWORD']
data it will post the correct data, but if I want to leave another review through a different $_SESSION
variable, once it is added into MySQL it displays the value of the first review I entered $_SESSION['KEYWORD']
in this example... 从下面的代码中,如果我使用
$_SESSION['KEYWORD']
数据在网站中输入评论,它将发布正确的数据,但是如果我想通过其他$_SESSION
变量留下另一个评论,则添加该评论进入MySQL时,它将显示我在此示例中输入$_SESSION['KEYWORD']
的第一条评论的值...
<?php session_start(); ?>
<?php include('db_connect.inc');?>
<link rel="stylesheet" type="text/css" href="style.css" />
<?php
if (isset($_SESSION['SUBURB'])) {
$VAR = $_SESSION['SUBURB'];
Set_Review($VAR);
unset($VAR);
} elseif (isset($_SESSION['KEYWORD'])) {
$VAR = $_SESSION['KEYWORD'];
Set_Review($VAR);
unset($VAR);
} elseif (isset($_SESSION['LOCATION'])) {
$VAR = $_SESSION['LOCATION'];
Set_Review($VAR);
unset($VAR);
} elseif (isset($_SESSION['RATING'])) {
$VAR = $_SESSION['RATING'];
Set_Review($VAR);
unset($VAR);
}
?>
<?php
function Set_Review($VAR) {
include('db_connect.inc');
$result = $conn->prepare("INSERT INTO dog_parks.reviews (review_text, username, date, rating, item) VALUES (:review, :id, :date, :rating, :park_value)");
$result->bindParam(':review', $value1);
$result->bindParam(':id', $value2);
$result->bindParam(':date', $value3);
$result->bindParam(':rating', $value4);
$result->bindParam(':park_value', $value5);
$value1 = $_POST['review'];
$value2 = $_SESSION['id'];
$value3 = $date = date("Y-m-d");
$value4 = $_POST['rating'];
$value5 = trim($VAR, "'\"");
$result->execute();
echo "<h3>Review entry Successful, you will now be redirected to the home page</h3>";
header( "refresh:5; url=index.php" );
unset($VAR);
}
?>
Pass by reference: function Set_Review(&$VAR)
. 通过引用传递:
function Set_Review(&$VAR)
。 Use "&" before $VAR. 在$ VAR之前使用“&”。
http://php.net/manual/en/language.references.pass.php http://php.net/manual/zh/language.references.pass.php
You will pass the memory address of $_SESSION variable, and then you can unset($VAR) the way you want. 您将传递$ _SESSION变量的内存地址,然后可以按所需方式取消设置($ VAR)。
You are unsetting the $VAR
variable, not your $_SESSION
variable. 您正在设置
$VAR
变量,而不是$_SESSION
变量。 Try: 尝试:
unset($_SESSION['KEYWORD'];
I think unset()
ing the $_SESSION
array is a bad way to do it. 我认为
unset()
$_SESSION
数组是一种不好的方法。 Just call session_destroy()
只需调用
session_destroy()
http://php.net/manual/it/function.session-destroy.php http://php.net/manual/it/function.session-destroy.php
If you want to delete only a part of the session data, try to enclose all those data in an array: 如果您只想删除部分会话数据,请尝试将所有这些数据封装在一个数组中:
$_SESSION["deleteme"] = array();
$_SESSION["deleteme"]["val1"] = "someval";
Then: 然后:
unset($_SESSION["deleteme"]);
I have solved the issue.. 我已经解决了这个问题。
Under each statement I added unset($_SESSION['session name here']); 在每个语句下,我添加了unset($ _ SESSION ['session name here']);
<?php
if (isset($_SESSION['SUBURB'])) {
$VAR = $_SESSION['SUBURB'];
Set_Review($VAR);
unset($_SESSION['SUBURB']);
} elseif (isset($_SESSION['KEYWORD'])) {
$VAR = $_SESSION['KEYWORD'];
Set_Review($VAR);
unset($_SESSION['KEYWORD']);
} elseif (isset($_SESSION['LOCATION'])) {
$VAR = $_SESSION['LOCATION'];
Set_Review($VAR);
unset($_SESSION['LOCATION']);
} elseif (isset($_SESSION['RATING'])) {
$VAR = $_SESSION['RATING'];
Set_Review($VAR);
unset($_SESSION['RATING']);
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.