User Login Script with Session

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ditch
    New Member
    • Oct 2006
    • 12

    User Login Script with Session

    Hi

    I am using the following code to get users to enter their username and password, it is then checked and a new page is displayed with their username. My question is, how can I display the rest of their details from that session, I have googled and read many articles, but I am still not any futher forward, any help would be much appreciated.
    [code=php]
    <?php
    session_start() ;
    $errorMessage = '';
    if (isset($_POST['txtUsername']) && isset($_POST['txtPassword'])) {
    include 'includes/config.php';
    include 'includes/opendb.php';

    $username = $_POST['txtUsername'];
    $password = md5($_POST['txtPassword']);

    // check if the user id and password combination exist in database
    $sql = sprintf("SELECT username
    FROM phpbb_users
    WHERE username = '%s'
    AND user_password = '%s'",$username ,$password);

    $result = mysql_query($sq l)
    or die('Query failed. ' . mysql_error());
    if (mysql_num_rows ($result) == 1) {
    // the user id and password match,
    // set the session
    $_SESSION['db_is_logged_i n'] = true;
    $_SESSION['username'] = $username;
    // after login we move to the main page
    header('Locatio n:main.php');
    exit;
    } else {
    $errorMessage = 'Sorry, wrong user id / password';
    }

    include 'includes/closedb.php';
    }
    ?>

    <html>
    <head>
    <title>Basic Login</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <?php
    include 'includes/header.php';
    include 'includes/navbar.tpl';
    if ($errorMessage != '')
    ?>
    <p> <strong><font color="#990000" ><?php echo $errorMessage; ?></font></strong></p>

    <form method="post" name="frmLogin" id="frmLogin">
    <table width="400" border="1" align="center" cellpadding="2" cellspacing="2" bordercolor="#0 00000">
    <tr>
    <td width="150">Use r Name</td>
    <td><input name="txtUserna me" type="text" id="txtUsername "></td>
    </tr>
    <tr>
    <td width="150">Pas sword</td>
    <td><input name="txtPasswo rd" type="password" id="txtPassword "></td>
    </tr>
    <tr>
    <td width="150">&nb sp;</td>
    <td>
    <input type="submit" name="btnLogin" value="Login">
    </div></td></tr>
    </table>

    </form>
    </body>
    </html>
    [/code]
    Originally posted by Ajaxrand
    Make use of the available formatting tags in the forum Use CODE tags around your code as appropriate:
    • [code]..code goes here..[ /code]
    • [code=php] ..php code goes here.. [ /code]
    • [code=c] ..C like code goes here.. [ /code]
    • [code=html] ..html code goes here.. [ /html]
    Last edited by ak1dnar; Jun 30 '07, 12:33 PM. Reason: Missing [CODE] Tags
  • ditch
    New Member
    • Oct 2006
    • 12

    #2
    Here is what is coded for the main page
    [code=php]
    <?php
    session_start() ;
    include 'includes/header.php';
    include 'includes/navbarout.tpl';

    // is the one accessing this page logged in or not?
    if (!isset($_SESSI ON['db_is_logged_i n'])
    || $_SESSION['db_is_logged_i n'] !== true) {

    // not logged in, move to login page
    header('Locatio n: login.php');
    exit;
    }

    echo sprintf("Welcom e to the Revenge's members area, you are logged in as %s",$_SESSION['username']);

    ?>
    [/code]
    Last edited by ak1dnar; Jun 30 '07, 12:24 PM. Reason: [CODE] Tags Missing

    Comment

    • ak1dnar
      Recognized Expert Top Contributor
      • Jan 2007
      • 1584

      #3
      Thread Title changed : Ajaxrand
      From >>> Help for a n00b, whilst using sessions
      To >>> User Login Script with Session

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        You have created two session Variables so far,Here.
        [code=php]
        // set the session
        $_SESSION['db_is_logged_i n'] = true;
        $_SESSION['username'] = $username;
        [/code]

        You Can use this $_SESSION['username'] to get the other records for the logged user.

        Other_page.php
        [code=php]
        session_start() ;
        $Logged_in_user = $_SESSION['username'];
        [/code]

        Now the the $Logged_in_user is here in this variable.
        re-use it for any data manipulation,If this user name is a Qnique One


        Thanks !

        Comment

        • ditch
          New Member
          • Oct 2006
          • 12

          #5
          Thank you for the reply, I understand, what you are saying, however, how can extract the data from multiple tables with that one variable

          Comment

          • kovik
            Recognized Expert Top Contributor
            • Jun 2007
            • 1044

            #6
            Originally posted by ditch
            Thank you for the reply, I understand, what you are saying, however, how can extract the data from multiple tables with that one variable
            Depends on how the tables are related. Typically, all tables dealing with a user are somehow related by the user id.

            [code=sql]SELECT `users`.`userna me`, `profiles`.`dat eJoined` FROM `users` LEFT JOIN (`profiles`) ON (`profiles`.`us erid` = `users`.`id`) WHERE `users`.`userna me` = $foo;[/code]

            Comment

            • ditch
              New Member
              • Oct 2006
              • 12

              #7
              Understand now, however the query I used looks like the :

              [PHP]$query = "SELECT phpbb_users.use rname, phpbb_users.use r_from, phpbb_users.use r_email, phpbb_ranks.ran k_image
              FROM phpbb_users
              LEFT JOIN (phpbb_ranks)
              ON (phpbb_users.us er_rank = phpbb_ranks.ran k_id)
              WHERE phpbb_users.use rname = $Logged_in_user ";[/PHP]

              When I run it, this error appears, which I believe means that it's not connecting to the DB:

              Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL result resource in

              If I change the variable $Logged_in_user to phpbb_users, everybodies details appear, so any ideas whats wrong with the query?

              Comment

              • ditch
                New Member
                • Oct 2006
                • 12

                #8
                HOOAH, done it thank you for all your help, I understand now, I chnaged the query to

                [PHP]$query = sprintf("SELECT phpbb_users.use rname, phpbb_users.use r_from, phpbb_users.use r_email, phpbb_ranks.ran k_image
                FROM phpbb_users
                LEFT JOIN (phpbb_ranks) ON (phpbb_users.us er_rank = phpbb_ranks.ran k_id)
                WHERE phpbb_users.use rname = '%s'",$Logged_i n_user);[/PHP]

                Comment

                • ak1dnar
                  Recognized Expert Top Contributor
                  • Jan 2007
                  • 1584

                  #9
                  ditch,

                  Since you have asked unrelated Question within the same thread, regarding PHP based calender I have to Split the thread.
                  Please Read Why
                  New Thraed Location:

                  Thanks ,
                  Originally posted by Ajaxrand
                  .

                  Comment

                  • ak1dnar
                    Recognized Expert Top Contributor
                    • Jan 2007
                    • 1584

                    #10
                    Copied Contents from Splitted thread -Ajaxrand

                    Originally posted by volectricity
                    By the way, the only problem with the first query was that you forgot the single quotes around the value.

                    Comment

                    Working...