douxian9060 2013-03-20 20:25
浏览 44
已采纳

php会话注册并在另一个页面中获取值

I am searching for a code that let the username of the account shows in welcome.php

my checklogin.php code is here :

 <?php
            mysql_connect('localhost', 'root', '');
            mysql_select_db('bloodneeded');


            $username = $_POST['uname'];
            $password = $_POST['pass'];


            $sql = "SELECT * FROM welcome WHERE username= '$username' AND  password= '$password'";
            $result = mysql_query($sql);
            if ($result) {

                $count = mysql_num_rows($result);

                if ($count == 1) {

                    session_register("uname");
                    session_register("pass");
                    header("location:Welcome.php");
                } else {

                    echo "Wrong Username or Password";
                }
            }
            ?>

and the welcome.php code is here :

<p id="form1"> Welcome  </p>

all i want is to "echo" near welcome the name of the user logged in can ayone please help me ?

  • 写回答

3条回答 默认 最新

  • drn34916 2013-03-20 20:32
    关注

    Use of session_register() is deprecated.

    Instead, use $_SESSION[] Superglobal.

    <?php
    
    //You must write
    if (!isset($_SESSION)) 
    {
        session_start();
    }
    //on very first lines of your pages.
    
    
            mysql_connect('localhost', 'root', '');
            mysql_select_db('bloodneeded');
    
    
            $username = $_POST['uname'];
            $password = $_POST['pass'];
    
    
            $sql = "SELECT * FROM welcome WHERE username= '$username' AND  password= '$password'";
            $result = mysql_query($sql);
            if ($result) {
    
                $count = mysql_num_rows($result);
    
                if ($count == 1) {
    
                    $_SESSION["uname"] = $_POST['uname'];
                    //$_SESSION["pass"] = $_POST['pass']; No need to store password I think.
                    header("location:Welcome.php");
                } else {
    
                    echo "Wrong Username or Password";
                }
            }
            ?>
    

    After getting the username

    <p id="form1"> Welcome <?php echo $_SESSION["uname"]; ?> </p>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?