dragon7713 2017-04-07 10:35
浏览 23

如何获取会话的字段ID?

I am new at php and I followed some youtube video's to build a littel project.

I have a table of users:

id
username
email
password

and I'm trying to build a private message system. I want to get the id of the user that is now logged in. But when I write my_id = $_Session[ 'id' ] I get an error : "Undefined index: id" but when I write $my_id = $_SESSION['username'] I get the username with no error and it echo's me the username. What is the difference?

That is all the code:

<?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.";
    }
?>

Update: done this but still is doesn't work. doesn't identify the id. this is the 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";
    }
}

?>

  • 写回答

2条回答 默认 最新

  • dpwjx32578146 2017-04-07 10:44
    关注

    You may haven't set it to the session. Use print_r($_SESSION); and check whether your index -> id is there or not.

    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示