会话变量问题 [英] Session variable problem

查看:66
本文介绍了会话变量问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在本网站页面之间传播会话变量时遇到问题

: http://www.meettheancestors.com/sessiontest/index.php

如果您输入任何用户ID和密码并单击登录(不执行实际的

验证),然后移动其他页面和/或

keep刷新页面它最终会显示一些不正确的信息,即当你不是,或者反之亦然时说你登录。

完全相同的代码在这里 http://ccgi.gnosis.free-online.co.uk/index .php

工作得很好意味着第一次PHP安装有问题。

这是3个文件的代码:

=========== index.php =============

<?php

session_start() ;

if(isset($ _ POST [''userid''])&& isset($ _ POST [''password''])){

//如果用户刚尝试登录

$ userid = $ _POST [''userid ''];

$密码= $ _POST [''密码''];

//假设有效登录所以设置会话var

$ _SESSION [''loggedinusername''] = $ userid;

}

?>

< html>

< body>

< h1>主页< / h1>

< ;?

if(isset($ _ SESSION) [''loggedinusername''])){

echo''您已登录为:''。$ _ SESSION [''loggedinusername'']。''< br /


>'';



echo''< a href =" logout.php">退出< / a>< br />'';

} else {

if(isset($ userid)){

//如果他们尝试过但未能登录

echo''无法登录。< br />'';

}其他{

//他们还没有尝试登录还是已经退出

echo''你还没有登录。< br />'';

}


//提供登录表格

echo''< form method =" post" action =" index.php">'';

echo''< table>'';

echo''< tr>< td> Userid:< / td>'';

echo''< td>< input type =" text" name =" userid">< / td>< / tr>'';

echo''< tr>< td>密码:< / td>'';

echo''< td>< input type =" password" name =" password">< / td>< / tr>'';

echo''< tr>< td colspan =" 2" align =" center">'';

echo''< input type =" submit" value ="登录">< / td>< / tr>'';

echo''< / table>< / form>'';

}

?>

< br />

< a href =" members_only.php"> ;会员部分< / a>

< / body>

< / html>


===== ====== members_only.php =============

<?php

session_start();

echo''< h1>仅限会员< / h1>'';

//检查会话变量

if(isset($ _ SESSION ['' loggedinusername''])){

echo''< p>您以''。$ _ SESSION [''loggedinusername'']登录。''< /

p>'';

echo''< p>会员只有内容到这里< / p>'';

}其他{

echo''< p>您尚未登录。< / p>'';

echo''< p>只有登录的会员可能会看到此页。< ; / p>'';

}


e cho''< a href =" index.php">返回主页< / a>'';

?>


=========== logout.php =============

<?php

session_start() ;

//存储以测试*是否*登录

$ old_user = $ _SESSION [''loggedinusername''];

unset($ _ SESSION [''loggedinusername'']);

session_destroy();

?>

< html>

< body>

< h1>退出< / h1>

<?php

if( !empty($ old_user)){

echo''已注销。< br />'';

} else {

//如果他们没有登录但是以某种方式来到这个页面

echo''你没有登录,所以还没有登出。< br /
< blockquote class =post_quotes>
>'';



}

?>

< a href =" index.php">返回到主页< / a>

< / body>

< / html>

========= ===============================


我做错了什么或是

http的PHP服务器上出现问题或配置问题://www.meettheancestors.com/phpinfo.php (我的
无法控制)。


任何帮助非常感谢。


Jonathan Attree

解决方案

_POST [''userid''])& &安培; isset(


_POST [''password''])){

//如果用户刚刚尝试登录

userid =


Hi

I am having a problem with session vars being propagated between pages
on this site:
http://www.meettheancestors.com/sessiontest/index.php
If you enter any user id and password and click Log In (no actual
validation is performed), and then move around the other pages and/or
keep refreshing the pages it will eventually display something that is
incorrect i.e. saying your logged in when you aren''t or vice versa.
The exact same code here http://ccgi.gnosis.free-online.co.uk/index.php
works fine implying a problem with the first PHP installation.
Here is the code for the 3 files:
=========== index.php =============
<?php
session_start();
if (isset($_POST[''userid'']) && isset($_POST[''password''])) {
// if the user has just tried to log in
$userid = $_POST[''userid''];
$password = $_POST[''password''];
//assume valid login so set session var
$_SESSION[''loggedinusername''] = $userid;
}
?>
<html>
<body>
<h1>Home page</h1>
<?
if (isset($_SESSION[''loggedinusername''])) {
echo ''You are logged in as: ''.$_SESSION[''loggedinusername''].'' <br /

>'';

echo ''<a href="logout.php">Log out</a><br />'';
} else {
if (isset($userid)) {
// if they''ve tried and failed to log in
echo ''Could not log you in.<br />'';
} else {
// they have not tried to log in yet or have logged out
echo ''You are not logged in.<br />'';
}

// provide form to log in
echo ''<form method="post" action="index.php">'';
echo ''<table>'';
echo ''<tr><td>Userid:</td>'';
echo ''<td><input type="text" name="userid"></td></tr>'';
echo ''<tr><td>Password:</td>'';
echo ''<td><input type="password" name="password"></td></tr>'';
echo ''<tr><td colspan="2" align="center">'';
echo ''<input type="submit" value="Log in"></td></tr>'';
echo ''</table></form>'';
}
?>
<br />
<a href="members_only.php">Members section</a>
</body>
</html>

=========== members_only.php =============
<?php
session_start();
echo ''<h1>Members only</h1>'';
// check session variable
if (isset($_SESSION[''loggedinusername''])) {
echo ''<p>You are logged in as ''.$_SESSION[''loggedinusername''].''</
p>'';
echo ''<p>Members only content goes here</p>'';
} else {
echo ''<p>You are not logged in.</p>'';
echo ''<p>Only logged in members may see this page.</p>'';
}

echo ''<a href="index.php">Back to main page</a>'';
?>

=========== logout.php =============
<?php
session_start();
// store to test if they *were* logged in
$old_user = $_SESSION[''loggedinusername''];
unset($_SESSION[''loggedinusername'']);
session_destroy();
?>
<html>
<body>
<h1>Log out</h1>
<?php
if (!empty($old_user)) {
echo ''Logged out.<br />'';
} else {
// if they weren''t logged in but came to this page somehow
echo ''You were not logged in, and so have not been logged out.<br /

>'';

}
?>
<a href="index.php">Back to main page</a>
</body>
</html>
========================================

Am I doing something wrong or is there a problem or config issue with
the PHP server at http://www.meettheancestors.com/phpinfo.php (which I
have no control over).

Any help greatly appreciated.

Jonathan Attree

解决方案

_POST[''userid'']) && isset(


_POST[''password''])) {
// if the user has just tried to log in


userid =


这篇关于会话变量问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆