Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts

    session of username, Firstname is blank

    Code:
    <?
    session_start();
    $_SESSION['username']='$myusername';
    if(!session_is_registered(myusername)){
    header("location:index.php");
    }
    ?>
    
    <html>
    <body>
    <form action="insert.php" method="post">
    Firstname: <? $myusername?>
    checklogin.php page:
    Code:
    <?php
    require_once('connection.php');
    $tbl_name="members";
    
    // username and password sent from form
    $myusername=$_POST['myusername'];
    $mypassword=$_POST['mypassword'];
    
    // To protect MySQL injection (more detail about MySQL injection)
    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);
    
    $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);
    
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    
    if($count==1){
    // Register $myusername, $mypassword and redirect to file "login_success.php"
    session_register("myusername");
    session_register("mypassword");
    header("location:login_success.php");
    }
    else {
    echo "Wrong Username or Password";
    }
    ?>

  • #2
    New Coder
    Join Date
    Mar 2008
    Location
    Somerset, England
    Posts
    93
    Thanks
    0
    Thanked 10 Times in 10 Posts
    add session_start() to the top of your check login page

    then change your session_register statements as below. This is because session_register is deprecated as of PHP version 4.1.0

    PHP Code:
    <?php
    if($count==1){
    // Register $myusername, $mypassword and redirect to file "login_success.php"
    $_SESSION['myusername'] = $myusername;
    $_SESSION['mypassword'] = $mypassword;
    header("location:login_success.php");
    }
    else {
    echo 
    "Wrong Username or Password";
    }
    ?>
    ?>
    I wouldn't recommend storing a password in a session variable, if you have a page that requires the password again have the user re-input it.

  • #3
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    thanks, that did not work!

  • #4
    New Coder
    Join Date
    Mar 2008
    Location
    Somerset, England
    Posts
    93
    Thanks
    0
    Thanked 10 Times in 10 Posts
    remove $_SESSION['username']='$myusername'; from your login_success page, this is overwriting the value already set on the check login page.


  •  

    Posting Permissions

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