php - 登录系统中 undefined variable session

标签 php mysql

你好,我正在尝试开发一个登录系统,但我似乎在某个条件下遇到错误。这是我的代码:

//header.php



                   if($_SESSION['signed_in']){  
                        echo 'Hello' . $_SESSION['user_name'] . '. Not you? <a href="signout.php">Sign out</a>';  
                    }  
                    else{  
                        echo '<a href="signin.php">Sign in</a> or <a href="sign up">create an account</a>.';  
                    }   

//Signin.php
                   if(mysql_num_rows($result) == 0) 
                    { 
                        echo 'You have supplied a wrong user/password combination. Please try again.'; 
                    } 
                    else 
                    { 
                        //set the $_SESSION['signed_in'] variable to TRUE 
                        $_SESSION['signed_in'] = true; 
                        //we also put the user_id and user_name values in the $_SESSION, so we can use it at various pages 
                        while($row = mysql_fetch_assoc($result)) 
                        { 
                            $_SESSION['user_id']    = $row['user_id']; 
                            $_SESSION['user_name']  = $row['user_name']; 
                            $_SESSION['user_level'] = $row['user_level']; 
                        } 
                        echo 'Welcome, ' . $_SESSION['user_name'] . '. <a href="index.php">Proceed to the forum overview</a>.'; 
                    } 

错误指向第一个条件:if($_SESSION['signed_in']) 它说:

注意: undefined variable :C:\xampp\htdocs\Tutorials\Forum\header.php 第21行的_SESSION

我该如何纠正?

编辑:session_start() 包含在文档类型中的 header.php 文件的顶部,而 header.php 包含在 Signin.php 中

完整代码:

header.php

    <?php
        session_start();
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl">  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
        <meta name="description" content="A short description." />  
        <meta name="keywords" content="put, keywords, here" />  
        <title>PHP-MySQL forum</title>  
        <link rel="stylesheet" href="css/style.css" type="text/css">  
    </head>  
    <body>  
    <h1>My forum</h1>  
        <div id="wrapper">  
        <div id="menu">  
            <a class="item" href="/forum/index.php">Home</a> -  
            <a class="item" href="/forum/create_topic.php">Create a topic</a> -  
            <a class="item" href="/forum/create_cat.php">Create a category</a>  
            <div id="userbar">  
            <div id="userbar">
                <?php 
                    if($_SESSION['signed_in']){  
                        echo 'Hello' . $_SESSION['user_name'] . '. Not you? <a href="signout.php">Sign out</a>';  
                    }  
                    else{  
                        echo '<a href="signin.php">Sign in</a> or <a href="sign up">create an account</a>.';  
                    }  
                ?>
            </div>  
        </div>  
            <div id="content">  

Signin.php

<?php  
//signin.php  
include 'conn.php';  
include 'header.php';  
echo '<h3>Sign in</h3>';  
//first, check if the user is already signed in. If that is the case, there is no need to display this page  
if(isset($_SESSION['signed_in']) && $_SESSION['signed_in'] == true)  
{  
    echo 'You are already signed in, you can <a href="signout.php">sign out</a> if you want.';  
}  
else  
{  
    if($_SERVER['REQUEST_METHOD'] != 'POST')  
    {  
        /*the form hasn't been posted yet, display it 
          note that the action="" will cause the form to post to the same page it is on */  
        echo '<form method="post" action="">  
            Username: <input type="text" name="user_name" />  
            Password: <input type="password" name="user_pass">  
            <input type="submit" value="Sign in" />  
         </form>'; 
    } 
    else 
    { 
        /* so, the form has been posted, we'll process the data in three steps:  
            1.  Check the data  
            2.  Let the user refill the wrong fields (if necessary)  
            3.  Varify if the data is correct and return the correct response  
        */  
        $errors = array(); /* declare the array for later use */  
        if(!isset($_POST['user_name']))  
        {  
            $errors[] = 'The username field must not be empty.';  
        }  
        if(!isset($_POST['user_pass']))  
        {  
            $errors[] = 'The password field must not be empty.';  
        }  
        if(!empty($errors)) /*check for an empty array, if there are errors, they're in this array (note the ! operator)*/  
        {  
            echo 'Uh-oh.. a couple of fields are not filled in correctly..'; 
            echo '<ul>'; 
            foreach($errors as $key => $value) /* walk through the array so all the errors get displayed */ 
            { 
                echo '<li>' . $value . '</li>'; /* this generates a nice error list */ 
            } 
            echo '</ul>'; 
        } 
        else 
        { 
            //the form has been posted without errors, so save it 
            //notice the use of mysql_real_escape_string, keep everything safe! 
            //also notice the sha1 function which hashes the password 
            $sql = "SELECT 
                        user_id, 
                        user_name, 
                        user_level 
                    FROM 
                        users 
                    WHERE 
                        user_name = '" . mysql_real_escape_string($_POST['user_name']) . "' 
                    AND 
                        user_pass = '" . sha1($_POST['user_pass']) . "'";  
            $result = mysql_query($sql);  
            if(!$result)  
            {  
                //something went wrong, display the error  
                echo 'Something went wrong while signing in. Please try again later.'; 
                //echo mysql_error(); //debugging purposes, uncomment when needed 
            } 
            else 
            { 
                //the query was successfully executed, there are 2 possibilities 
                //1. the query returned data, the user can be signed in 
                //2. the query returned an empty result set, the credentials were wrong 
                if(mysql_num_rows($result) == 0) 
                { 
                    echo 'You have supplied a wrong user/password combination. Please try again.'; 
                } 
                else 
                { 
                    //set the $_SESSION['signed_in'] variable to TRUE 
                    $_SESSION['signed_in'] = true; 
                    //we also put the user_id and user_name values in the $_SESSION, so we can use it at various pages 
                    while($row = mysql_fetch_assoc($result)) 
                    { 
                        $_SESSION['user_id']    = $row['user_id']; 
                        $_SESSION['user_name']  = $row['user_name']; 
                        $_SESSION['user_level'] = $row['user_level']; 
                    } 
                    echo 'Welcome, ' . $_SESSION['user_name'] . '. <a href="index.php">Proceed to the forum overview</a>.'; 
                } 
            } 
        } 
    } 
} 


include 'footer.php';  
?>  

最佳答案

在将某些内容存储到 session 中之前,变量 $_SESSION['signed_in'] 将为空。出现警告是因为您要求提供此值,但里面没有任何内容。

if (isset($_SESSION['signed_in']) && $_SESSION['signed_in'])
{
}

为避免警告,您应该首先检查变量是否存在,然后您可以从中读取。当然这有点麻烦,所以大多数开发人员创建一个函数只是为了从数组中安全读取。

编辑:

实际上上面的问题会导致另一个消息...

Notice: Undefined index: ...

...所以正如Mob所说,变量$_SESSION根本不存在,因为没有启动 session 。我会让这个答案留下来,因为这将是下一个陷阱。

关于php - 登录系统中 undefined variable session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12931503/

相关文章:

php - 将带引号的字符串发送到 javascript 函数,然后发送到 php

mysql - 添加不需要的比较以获得结果但减少选择是一个好主意吗?

mysql - SSRS 报告数据库版本之间的被动性

php - Blade 引擎元素渲染排序

PHP更改加载页面链接样式

php - 从 url 或 html 获取所有图像名称并保存在文件夹中

javascript - 同页面div变化不刷新

php - mySQL - 匹配拉丁(英语)表单输入到 utf8(非英语)数据

php - 从 mysql 获取数组的反转结果

php - 评论数组/循环不循环