SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    UK
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    session help needed

    Hi all,

    I've got the following script which is a login form which checks for entries in a mysql database.

    Once the user has successfully logged in, it displays their username and password. (session_register etc)

    I can display the username and password, but I cant get it to display the firstname column entry in the database. Is there something I'm doing wrong?

    Here's the code:
    PHP Code:
    <?
    session_start
    ();

    if (
    $user && $pass)
    {
      
    // if the user has just tried to log in

      
    $db_conn mysql_connect("localhost""root""");
      
    mysql_select_db("mdgal"$db_conn);
      
    $query "select user, pass, firstname from users WHERE user='$user' and pass='$pass'";
      
    $result mysql_query($query$db_conn);
      if (
    mysql_num_rows($result) >)
      {
        
    // if they are in the database register the user id
        
    $valid_user $user;
        
    session_register("valid_user");
        
        
    $valid_pass $pass;
        
    session_register("valid_pass");
        
        
    $valid_firstname $firstname;
        
    session_register("valid_firstname");

      }
    }
    ?>

    <html>
    <head>
    <title>MdGal - (Template)</title>

    </head>

    <body leftmargin="0" topmargin="0">
    <table width="100%" height ="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="85"><?php include("includes/header.php"); ?></td>
      </tr>
      <tr>
        <td height="*" valign="top">
        <!-- Begin of  main content area  -->
        <table width="100%" height ="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="150" valign="top"><?php include("includes/side_nav.php"); ?></td>
        
              <td width="*" valign="top">&nbsp;



    <? 

      
    if (session_is_registered("valid_user"))
      {
        echo 
    "Your username is: $valid_user <br>";
        echo 
    "Your password is: $valid_pass <br>";
        echo 
    "Your firstname is: $valid_firstname <br>";
        
        
        echo 
    "<a href=\"logout.php\">Log out</a><br>";
      }
      else
      {
        if (isset(
    $user))
        {
     
          echo 
    "Could not log you in";
        }
        else 
        {

          echo 
    "You are not logged in.<br>";
        }

        
    // provide form to log in 
        
    echo "<form method=post action=\"login.php\">";
        echo 
    "<table>";
        echo 
    "<tr><td>Userid:</td>";
        echo 
    "<td><input type=text name=user></td></tr>";
        echo 
    "<tr><td>Password:</td>";
        echo 
    "<td><input type=password name=pass></td></tr>";
        echo 
    "<tr><td colspan=2 align=center>";
        echo 
    "<input type=submit value=\"Log in\"></td></tr>";
        echo 
    "</table></form>";
      }
    ?>
    <br>
    <a href="members_only.php">Members section</a>
               
              
              </td>
        
            </tr>
          </table>
         <!-- End of  main content area  -->
          </td>
      </tr>
      <tr>
        <td height="30">
        <?php include("includes/footer.php"); ?>
        </td>
      </tr>
    </table>
    </body>
    </html>
    Can anyone help?.... please

    Matt

  2. #2
    SitePoint Zealot LiamW's Avatar
    Join Date
    Mar 2003
    Location
    Hamilton, Ontario
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Which verison of php are you using? If you are using php >=4.1, I'd switch over from session_register to the new $_SESSION array.
    To read more about session_register and the new $_SESSION array click here

    Anyhoo your problem is that you aren't performing a query on the database for the result of the firstname. The user and pass are being sent from the form which is why it checks out.
    Do something like

    PHP Code:
    $db_conn mysql_connect("localhost""root""" );
      
    mysql_select_db("mdgal"$db_conn);
      
    $query "select user, pass, firstname from users WHERE user='$user' and pass='$pass'";
      
    $result mysql_query($query$db_conn);
      
    //get firstname from db and store in variable
      
    $rows mysql_fetch_array($result);
      
    $firstname $rows['firstname'];
      if (
    mysql_num_rows($result) >)
      {
        
    // if they are in the database register the user id
        
    $valid_user $user;
        
    session_register("valid_user" );
        
        
    $valid_pass $pass;
        
    session_register("valid_pass" );
        
        
    $valid_firstname $firstname;
        
    session_register("valid_firstname" ); 

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    UK
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again LiamW

    All working cool now...

    Matt


Bookmarks

Posting Permissions

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