如何获取会话的字段ID?

时间:2017-04-07 10:35:23

标签: php

我是php的新手,我跟着一些youtube视频来构建一个小型项目。

我有一个用户表:

id
username
email
password

我正在尝试构建一个私人消息系统。我想获得现在登录的用户的id。但是当我写my_id = $_Session[ 'id' ]时,我收到错误:"Undefined index: id"但是当我写$my_id = $_SESSION['username']时,我得到了没有错误的用户名,它回显了我的用户名。有什么区别?

这就是所有代码:

<?php
    session_start();
    $db = mysqli_connect( "localhost", "root", "", "travelersdb" );

    if( @$_SESSION[ "username" ] )
    {
        ?>

        <html>
            <head>
                <title>Home Page</title>
            </head>

            <?php
                include( "header.php" );
                echo "<center><h1>Private Message System</h1>";
                include( "message_title_bar.php" );

                if( isset( $_GET[ "user" ] ) && !empty( $_GET[ "user" ] ) )
                {
                    ?>

                    <form method = 'post' >

                        <?php
                            if( isset( $_POST[ "message"] ) && !empty( $_POST[ "message" ] ) )
                            {
                                $user=$_GET['user'];

                                $my_id = $_SESSION['username']; // ------> Doesn't work when changed to 'id'

                                $random_number = rand();      
                                $sql_m = "SELECT 'hash'
                                          FROM message_group
                                          WHERE ( user_one = '" . $my_id . "' AND user_two = '" . $user . "' )
                                             OR ( user_one = '" . $user . "' AND user_two = '" . $my_id . "' )";    

                                $check_con = mysqli_query( $db, $sql_m );
                                $rows = mysqli_num_rows( $check_con );
                                if( $rows == 1 )
                                {
                                    echo "<p>Conversation already started!</p>";
                                }
                                else
                                {    
                                    echo $user . "</br>";
                                    echo $my_id; -------> Wanted to echo the $my_id to check...
                                    echo $random_number;
                                    // $sql_In = "INSERT INTO message_group( user_one, user_two, hash ) 
                                    //            VALUES( '1111', '2222', '2222' )";  
                                    // mysqli_query( $db, $sql_In );
                                    echo "<p>Conversation Started</p>";
                                }
                            }
                        ?>

                        Enter Message : </br>

                        <textarea name = 'message' rows = '7' cols = '60'>
                        </textarea>

                        <br></br>

                        <input type='submit' value="Send Message" />
                    </form>

                    <?php
                }
                else
                {
                    echo "<b>select user</b>";
                    $sql = "Select id,username from users";
                    $check = mysqli_query($db,$sql);
                    while ( $run_user = mysqli_fetch_array( $check ) )
                    {
                        $user = $run_user[ 'id' ];
                        $username = $run_user[ 'username' ];
                        echo "<p><a href = 'send.php?user=$user'>$username</a></p>";    
                    }
                } 
            ?>
        </html>

        <?php
    }
    else
    {
        echo "You must be logged in.";
    }
?>

更新:完成此操作但仍然无效。不识别id。这是login.php:

<?php
session_start();
//connect to database
$db=mysqli_connect("localhost","root","","travelersdb");
if(isset($_POST['login_btn']))
{
    //$username=mysql_real_escape_string($_POST['username']);
    //$password=mysql_real_escape_string($_POST['password']);

    $username = mysqli_real_escape_string($db, $_POST['username']);
    $password = mysqli_real_escape_string($db, $_POST['password']);

    $password=md5($password); //Remember we hashed password before storing last time
    $sql="SELECT * FROM users WHERE username='$username' AND password='$password'";
    $result=mysqli_query($db,$sql);
    if(mysqli_num_rows($result)==1)
    {
        $_SESSION['message']="You are now Loggged In";
        $_SESSION['username']=$username;
        $sql_t = "select id from users where username='$username' AND password='$password'";
        $id = mysqli_query($db, $sql_t);
        $_SESSION['id']=$id;
        header("location:index.php");
    }
   else
   {
                $_SESSION['message']="Username and Password combiation incorrect";
    }
}

&GT;

2 个答案:

答案 0 :(得分:0)

您可能无法将其设置为会话。 使用print_r($_SESSION);并检查您的索引是否为&gt; id是否存在。

答案 1 :(得分:0)

$ _ SESSION索引就像会话值的变量名。如果您之前没有创建此会话/变量,则无法检索其值,因为它未定义