变量$ _SESSION不希望被设置

时间:2013-12-07 22:33:47

标签: php session login

所以这是页面index.php的代码:$ _SESSION [“username”]变量似乎没有设置,我不知道为什么因为在登录页面我使用isset控件并且登录成功如果我输入正确的值;如果输入错误的用户名和密码则不然。我知道我应该用md5“编码”密码但是现在这不是我的问题:( 正如您所看到的,我在登录后重定向到索引页面。如果用户已经登录,我将从索引页面重定向到“home.php”页面。问题是,在登录后,它会一直显示登录表单,并且不会将我重定向到home.php。

<?php session_start(); 
        require_once "dbConn.php"; dbconnect();
        if(isset($_SESSION["username"])){
        echo $_SESSION["username"]; // TEST it never enters THERE!!!
        echo'<p>Trasferimento alla home page</p>';
    header("Refresh: 2; URL = home.php");
    }

    else{
    echo'<div id=\"container\">';
    echo' 
        <div id=\"content\">
        <h2> You need to login :</h2>
        <br/>
        <form id="form1" name="form1" method="post" action="login.php">
        <input type="text" name="username" id="username" />
        <input type="password" name="password" id="password" />
        <input type="submit" name="accedi" id="accedi" value="Accedi" /> 
        </form>
        <br/>
        </div>';
    include 'Footer.php';
    echo'</div>';
    }?>

这是login.php页面:

<?php
require_once "dbConn.php"; dbconnect();
if(isset($_POST['username']) && isset($_POST['password'])) {
    $username=mysql_real_escape_string($_POST['username']);
    $pwd = mysql_real_escape_string($_POST['password']);
    $query = mysql_query("SELECT * FROM user WHERE username='$username' AND password ='$pwd';");  

    if(mysql_num_rows($query) == 1){
    $sessione =mysql_fetch_array($query);
    $_SESSION["username"] = $sessione["username"];  
    echo $_SESSION["username"]; //TEST - it prints what I want: my username
    $_SESSION["logged"] = true;
    echo'Login effettuato con successo!';
    header("Refresh: 2; URL = index.php");
    }
    else if((mysql_num_rows($query) == 0)){
        echo'Utente non registrato o password errata';
        header("Refresh: 2; URL = index.php");
    }
}
?>  

Thx all ;)

1 个答案:

答案 0 :(得分:2)

您忘记在登录页面上致电session_start()

<?php
require_once "dbConn.php"; dbconnect();

应该是

<?php
session_start()
require_once "dbConn.php"; dbconnect();