简体   繁体   English

PHP MySQL登录未保存会话数据

[英]PHP MySQL Login not saving Session data

I am using this PHP code for a login script: 我正在使用此PHP代码作为登录脚本:

<?php
if(isset($_POST["submit"]))
{
    session_start();
    //get the username, password and keyword sent from the form
    $username=$_POST['username']; 
    $password=$_POST['password'];

    //check in the database to see if the username, password and keyword match in the database
    $sql="SELECT * FROM admin WHERE username=('$username') and password=MD5('$password')";
    $rs=mysql_query($sql,$conn) or die(mysql_error());
    $result=mysql_fetch_array($rs);

    $check="SELECT * from admin where username='$username' ";
    $check2=mysql_query($check,$conn) or die(mysql_error());
    $check3=mysql_fetch_array($check2); 
    if($check3["logintries"] > '3')
    {
        echo '<p align="center"><h4>Your account has been suspended due to too many failed logins. Please contact support</h4></p>';
    }
    else
    {
        //get the number of rows that match in the database
        $count=mysql_num_rows($rs);

        //if the number of rows equals 1, then create the session variables
        if($count==1)
        {
            //$sql="INSERT into user_logins (user_seq, timestamp, ip_address, posted_username, posted_password, posted_keyword) values ('".$result["sequence"]."', '".date("Y-m-d H:i:s")."', '".$_SERVER["REMOTE_ADDR"]."', '".$username."', '".$password."', '".$keyword."') ";
            //$rs=mysql_query($sql,$conn) or die(mysql_error());

            session_start();
            $_SESSION["sequence"]=$result["sequence"];
            $_SESSION["loggedin"]='yes';
            echo $_SESSION["loggedin"];
            $_SESSION["ipaddress"]=$_SERVER["REMOTE_ADDR"];

            //then redirect to the main page
            //header("location: index.php");
            echo '<h3>Login Has Been Successful - Please wait while we redirect you...</h3>';
            //echo '<meta http-equiv="refresh" content="0;URL=index.php" />';
        }
        else 
        {
            $sql="SELECT * FROM admin WHERE username='".$_POST["username"]."' ";
            $rs=mysql_query($sql,$conn) or die(mysql_error());
            $result=mysql_fetch_array($rs);
            $logintries=$result["logintries"];
            $sql2="UPDATE admin set logintries = '".($logintries+1)."' where username = '".$result["username"]."' ";
            $rs2=mysql_query($sql2,$conn) or die(mysql_error());
            //other wise display an error message
            //echo '<p align="center"><h4>Username or Password incorrect</h4></p>';
        }
    }
}

?>

as you can se i have echoed the session variable "loggedin" and it echoes as yes on the page but when it goes to the member.php page its not keeping the variables. 如您所知,我已经回显了会话变量“ loggedin”,并且在页面上以“ yes”回显,但是当它转到member.php页面时,它不保留变量。

On the member.php page i have included an authorisation.php which has this PHP Code: 在member.php页面上,我包含了一个具有以下PHP代码的authorization.php:

<?php
if($_SESSION["loggedin"] != 'yes')
{
    header("Location: /admin/login.php");
}
?>

so if there is no session variable "loggedin" with a value of "yes" it will just redirect back to the login.php page. 因此,如果没有值为“ yes”的会话变量“ loggedin”,它将仅重定向回login.php页面。

as explained above, it is not storing the variable for some reason. 如上所述,由于某种原因它没有存储变量。

This is exactly the same code i have on another website which works fine, the only thing i have changed is the database login details. 这与我在另一个网站上正常运行的代码完全相同,我唯一更改的是数据库登录详细信息。

all the columns that are being selected i have checked in the database and they are all correct columns (spelt correctly) 我已经在数据库中检查了所有选中的列,它们都是正确的列(正确拼写)

any ideas what it could be? 有什么想法吗?

Use session_start(); 使用session_start(); before using any session variable, then it will display all values. 在使用任何会话变量之前,它将显示所有值。

you must put session_start(); 您必须放置session_start(); at the start of the file. 在文件的开头。 best to put in the config file that you include in all files, so its there in all files. 最好放入所有文件中包含的config文件,因此所有文件中都包含该config文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM