我正在通过会话传递用户变量。它在localhost上工作正常,但一旦在Web服务器上它就会做出奇怪的事情。
登录后,会话变量按原样工作.....直到你点击大约三页然后突然变为POOF!
请注意“Welcome, jordan.
”而不是“Welcome, .
”也是左上角。
会议功能: http://imageshack.us/photo/my-images/32/loggedins.png/
会话POOF! http://imageshack.us/photo/my-images/515/loggedinno.png/
登录/创建会话变量代码:
<?php
if (!isset($_SESSION['user']))
{
if (isset($_POST['user']))
{
$user = sanitizeString($_POST['user']);
$pass = sanitizeString($_POST['pass']);
if (preg_match($txtMatch,$user))
{
if ($user == "" || $pass == "")
{
$error = "Please enter all required fields";
}
else
{
$query = "SELECT * FROM gtmembers WHERE user='$user'";
$result = queryMysql($query);
$rank = mysql_result($result, 0, 'rank');
if (!mysql_num_rows($result))
{
$error = "Username does not exist.";
}
else
{
$getPass = mysql_result($result, 0, 'pass');
$salt = substr($getPass, 0, 64);
$hash = $salt . $pass;
for ($i = 0; $i < 100000; $i++)
{
$hash = hash('sha256', $hash);
}
$hash = $salt . $hash;
if ($hash == $getPass)
{
if ($rank != "Banned")
{
$userLow = strtolower($user);
$_SESSION['user'] = $userLow;
$_SESSION['rank'] = $rank;
echo <<<_END
<script type="text/javascript">
window.location.href='index.php';
</script>
_END;
echo "Successfully logged in. Click <a href='index.php'>here</a> to continue.";
}
标题代码:
<?php //gtheader.php
session_start();
include_once 'gtfunctions.php';
$loggedIn = FALSE;
if (isset($_SESSION['user']))
{
$user = $_SESSION['user'];
if ($user) echo "Current User: $user<br />";
else echo "Current User: None<br />";
$rank = $_SESSION['rank'];
$loggedIn = TRUE;
echo "is set SESSION['user']? Yes";
}
else echo "is set SESSION['user']? No";
echo "<div id='header'><a class='header' href='index.php'> <h1 id='headerTitle'>$appname</h1></a>";
if ($loggedIn == TRUE)
{
$query = "SELECT * FROM gtmessages WHERE recip='$user' AND status='0'";
$result = queryMysql($query);
if (mysql_num_rows($result) == 0) $num = "";
else $num = "[".mysql_num_rows($result)."]";
if ($rank == 'Owner' || $rank == 'Admin')
{
echo "Welcome, <a class='header' href='gtmembers.php?view=$user'>$user</a><a class='header' href='gtmessage.php'>$num</a>. [<a class='header' href='gtlogout.php'>Logout</a>] | <a class='header' href='gtadmin.php'>Admin</a><br />";
}
else
{
echo "Welcome, <a class='header' href='gtmembers.php?view=$user'>$user</a><a class='header' href='gtmessage.php'>$num</a>. [<a class='header' href='gtlogout.php'>Logout</a>]<br />";
}
}
?>
答案 0 :(得分:1)
如果它在一个环境而不是另一个环境中工作,我猜你的PHP.ini在有效的环境中有session.auto_start = 1
。最佳做法是始终在页面顶部调用session_start()
,而不是依赖php.ini
进行正确设置。这应该可以在任何环境中使用。
答案 1 :(得分:1)
解决。
这里的问题是Host Gator注册了全局变量。因此,由于我使用$ user,并且$ _SESSION ['user']被注册,它会被覆盖。
我通过将所有文件中的$ _SESSION ['user']更改为$ _SESSION ['myUser']来修复此问题
感谢您的帮助。
答案 2 :(得分:0)
必须在页面顶部调用session_start()
<强> gtheader.php 强>
<?php
session_start();
include_once 'gtfunctions.php';
$loggedIn = FALSE;
.....