PHP - 刷新后会话丢失

时间:2016-01-20 20:40:20

标签: php session login

我在php中有问题。我已经实现了一个注册和登录的网站。在这一周,登录工作正常,但今天会议有问题......页面刷新后它们会丢失。我试图在堆栈溢出中找到答案,但我不能'吨。这是代码

<?php @session_start(); ?>
<?php require_once('Connections/connection.php'); ?>
<?php

mysql_select_db($database_connection, $connection);
$query_login2 = "SELECT * FROM `users`";
$login2 = mysql_query($query_login2, $connection) or die(mysql_error());
$row_login2 = mysql_fetch_assoc($login2);
$totalRows_login2 = mysql_num_rows($login2);



$loginFormAction = $_SERVER['PHP_SELF'];

if (isset($_POST['usernamelog'])) {
    $query_User = sprintf("SELECT * FROM `users` WHERE Email= %s", GetSQLValueString($_POST['usernamelog'], "text"));
    $User = mysql_query($query_User, $connection) or die(mysql_error());
    $row_User = mysql_fetch_assoc($User);
    $totalRows_User = mysql_num_rows($User);
    $username = $row_User['Email'];
    $statuss= $row_User['Status'];

    $usernamee = $_POST['usernamelog'];
    $password = $_POST['passwordlog'];
  mysql_select_db($database_connection, $connection);

  $LoginRS__query=sprintf("SELECT Status, Email, Password FROM `users` WHERE Email=%s AND Password=%s",
    GetSQLValueString($usernamee, "text"), GetSQLValueString($password, "text")); 

  $LoginRS = mysql_query($LoginRS__query, $connection) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  //echo $loginFoundUser;
  if ($loginFoundUser) {    
    $_SESSION['MM_Username'] = $usernamee;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;   

  }
}
$colname_User = "-1";
if (isset($_SESSION['MM_Username'])) {
  $_SESSION['MM_Usernamee'] = $_SESSION['MM_Username'];
}
/******$_SESSION['MM_Usernamee'] IS LOST AFTER REFRESH********/
    mysql_select_db($database_connection, $connection);
    $query_User = sprintf("SELECT * FROM `users` WHERE Email= %s", GetSQLValueString($_SESSION['MM_Usernamee'], "text"));
    $User = mysql_query($query_User, $connection) or die(mysql_error());
    $row_User = mysql_fetch_assoc($User);
    $totalRows_User = mysql_num_rows($User);
    $username = $row_User['Email'];
    $status= $row_User['Status'];

    ?>

P.S。在Localhost上运行正常。

正确答案在答案的评论中!!!

1 个答案:

答案 0 :(得分:1)

在旧版本的PHP中,您必须在设置会话变量之前使用方法session_register('[session_name]')

所以试试:

<?php
session_start();

if ( ! isset($_SESSION['session_name'])
{
    session_register('session_name');
    $_SESSION['session_name'] = 'Session value';
}

echo $_SESSION['session_name'];

编辑:在你写之前

<?php session_start()

你不能有任何文字,甚至没有空格。因为然后在会话可以设置自己的标头之前发送标头。 因此,您必须先检查PHP的启动是否是此脚本中首先加载的内容。 即使您使用“include”加载此脚本,也不能使用任何文本。