www.webdeveloper.com
Results 1 to 2 of 2

Thread: Cannot echo "username" from SESSION

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    2

    Question Cannot echo "username" from SESSION

    Hi here is the login page
    PHP Code:
    <?
    session_start
    ();  
    session_register("login_session");  
    $username "";
    $password "";

    if (isset(
    $_POST["Username"]))
       
    $username $_POST["Username"];
    if (isset(
    $_POST["Password"]))
       
    $password $_POST["Password"];

    if (
    $username != "" && $password != "") {

       
    $link mysql_connect("localhost","root","root");
       
    mysql_select_db("database"); 

       
    $sql "SELECT * FROM trainee WHERE password='";
       
    $sql.= $password."' AND username='".$username."'";
       
    $result mysql_query($sql); 
       if (
    mysql_fetch_row($result) != false) {

          
    $_SESSION["login_session"] = true;
          if (
    $username "trainee"){
          
    header("Location: student.php");}
        }else 
            echo 
    '<script type="text/javascript">
            window.onload=function(){alert("It is incorrect! Please enter again!");} 
            </script>'
    ;
       
    mysql_close($link);
    }
    ?>
    Below is the form:
    HTML Code:
    <div class="sidebar">
              <div class="sidebar_item" id="login_form">
                <p>&nbsp;</p>
                <form method="post" action="login_stu.php">
    			<fieldset>
    			<legend align="center">Trainee Login</legend>
                  <p>
                    <label for="ID"> Username:</label>
                    <input type="text" name="Username" />
                  </p>
                  <p>
                    <label for="PASSWORD"> Password:<br />
                    </label>
                    <input type="password" name="Password" />
                  </p>
    				<p align="center">
                    <input type="submit" name="submit" value="Login" style="height:1.7em; width:3.5em;"/>
    				</p>
    			  </fieldset>
                </form>
              </div> 
            </div> 
    I got another page requiring me to display the username
    And I have tried to use
    PHP Code:
    session_start(); 
    echo 
    $_SESSION['username'
    or
    PHP Code:
    session_start(); 
    echo 
    $username
    , etc.

    Any ideas? Thank you.

  2. #2
    Join Date
    Aug 2012
    Posts
    155
    You aren't assigning the username to a session variable anywhere, so you need to do so
    PHP Code:
    if (mysql_fetch_row($result) != false) { 

          
    $_SESSION["login_session"] = true;
          
    $_SESSION['username'] = $username

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles