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

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 ?

图片转代码服务由CSDN问答提供 功能建议

我正在搜索一个代码,让帐户的用户名显示在welcome.php

我的checklogin.php代码在这里:

 &lt;?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”); 
}其他{\  n 
 echo“用户名或密码错误”; 
} 
} 
?&gt; 
   
 
 

,welcome.php代码在此处:

 &lt; p id =“form1”&gt; 欢迎&lt; / p&gt; 
   
 
 

所有我想要的是“回声”接近欢迎登录用户的名字 can ayone请帮帮我?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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>
    
    打赏 评论
  • dongxia527680 2013-03-20 20:31

    Please note that session_register() function is DEPRECATED, you can use $_SESSION instead:

    1. first: you need to call session_start(); on top of all pages
    2. use: $_SESSION['uname'] = 'some value'; in first page.
    3. print back the session by: echo $_SESSION['uname']; in welcome page.
    打赏 评论
  • doubengshao8872 2013-03-20 20:39
    1. Don't use mysql_ its a deprecated extension. http://oleaass.com/why-people-needs-to-stop-using-the-mysql-extension/

    Use PDO instead. 2. I'm hoping that you just named that P tag id="form1" for show... I would never name it that.

    <?php
    session_start();
    
    $session_uname = '';
    if(!empty($_SESSION['uname']))
    {
       $session_uname = htmlspecialchars($_SESSION['uname'], ENT_QUOTES);
    } else
    {
       // some fancy redirect to login page
    }
    
    ?>
    
    <input type="text" id="username" value="<?php echo $session_uname; ?>" />
    
    打赏 评论

相关推荐 更多相似问题