[英]php Session variable lost between pages with a form submit
I was wondering if anyone could help me. 我想知道是否有人可以帮助我。
If I am on the page details.php (code below) there is a form with a submit button (amongst other things). 如果我在页面details.php(下面的代码)上,则有一个带有提交按钮的表单(除其他外)。
When the form's submit button is clicked, you are taken to process_tenant.php. 单击表单的“提交”按钮后,您将进入process_tenant.php。
The problem is the session variable valid_user doesn't seem to come with it. 问题是会话变量valid_user似乎不附带它。 I have session_start() at the start of both pages and the session variable is definitely set and never overwritten on details.php 我在两个页面的开头都有session_start(),并且session变量肯定已设置,并且从未在details.php上覆盖
Anyone able to tell me why? 有人能告诉我为什么吗?
details.php details.php
<?php
session_start();
$_SESSION['valid_user'] = 'admin';
echo $_SESSION['valid_user']; //This prints out admin
?>
<!DOCTYPE HTML>
<html>
<head>
...
</head>
<body>
...
<form action='process_tenant.php' method='POST'>
...
</form>
</body>
</html>
process_tenant.php process_tenant.php
<?php
session_start();
if (isset($_SESSION['valid_user']))
{
echo 'heya'; //This does not print
}
echo 'hi' . $_SESSION['valid_user']; //This just prints 'hi'
?>
...
In details.php the value admin doesn't equate to anything. 在details.php中,值admin不等于任何值。 It needs to be either a variable or a string or some kind of valid type otherwise it will be unset. 它必须是变量或字符串或某种有效类型,否则它将不被设置。
Try changing your code to this: 尝试将代码更改为此:
$_SESSION['valid_user'] = 'admin';
Config.php Config.php
<?php
ob_start();
session_start();
$_SESSION['valid_user'] = admin;
?>
Details.php Details.php
<?php
include('Config.php');
?>
process_tenant.php process_tenant.php
<?php
include('Config.php');
if (isset($_SESSION['valid_user']))
{
echo 'heya';
}
echo 'hi' . $_SESSION['valid_user'];
?>
仅启动一次,即仅使用session_start()
一次,从process_tenant.php
删除,然后告诉我,一旦完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.