Jump to content


Photo

Sessions help!


  • Please log in to reply
2 replies to this topic

#1 adiwood

adiwood
  • Members
  • Pip
  • Newbie
  • 2 posts

Posted 31 March 2006 - 02:11 PM

Hi, I have been trying to look at some of the session tutorials, although i always seem to get lost, since i dont know how to apply them to what i have. I have a login page, that the user fills in the username and password, a response page then validates the user, and redirects them to the correct page depending on their usertype - e.g username, admin, superadmin.

Where do i have to put the different parts of the session so that users can access to the admin page by typing it in.

Also, this code is rather long winded at the moment. How can i cut it down? I guess i need to do something with $blnHitError = true;

Sorry, but im totally lost on this!!

Below is my code:



<?php

    

    {

    $domain = "localhost";
    $username = "root";
    $password = "paxcroft";
    $database = "traceability";
    $connection = mysql_connect($domain, $username, $password);
    $database = mysql_select_db($database, $connection);

    $user = mysql_query("SELECT `Userid`,`UserType` ".
                            "FROM `Users` ".
                            "WHERE `UserName` = '".$_POST['username']."' ".
                            "AND `Password` = '".$_POST['password']."' ".
                            ";", $connection);

    $userRecord = mysql_fetch_array($user);
    $UserType = $userRecord["UserType"];

    if ($userRecord["Userid"] > 0) {
        $CurrentUser = $userRecord["Userid"];
        if (!$_POST['password'] = $userRecord["password"]) {
            if ($UserType == 'User') {
            session_register("$username","$password");
            header( "Location: user.php" );
            } elseif ($UserType == 'Admin') {
                session_register("$username","$password");
             header( "Location: admin.php" );
            } elseif ($UserType == 'SuperAdmin') {
                session_register("$username","$password");
             header( "Location: superadmin.php" );
            };

        };
    } else {
        ?>
        <html>
        <head>
        <title>Avon Technical Products Traceability System</title>

        <link rel=StyleSheet href="style.css" type="text/css">

        </head>

        <body>
        <form action="response.php" method="POST">

        <div id="wrapper">
            <div id = "header">
                <div id = "logo">
                    <img src="images/avon_logo.gif" alt="Avon Technical Products" title="Company logo" />
                </div>

                <div id = "pageInfo">Avon Technical Products Traceability System</div>
            </div>

            <div id="subNav">
                <li><A HREF="index.php"><div id="button">Try Again</div></a></li>
            </div>

            <div id = "content">
                <div id = "mainContent">
                    <div id = "title">LogOn</div>
            <div id = "Information">
              Sorry, unable to login. Please check your user name and password.
              </div>
                        </div>
                    </div>
                </div>
            </div>
            </form>
            </body>
        </html>
        <?php
    }

    mysql_close($connection);


}
?>


#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 31 March 2006 - 02:17 PM

I don't understand why you have 2 else statements or why there is a semicolon after the closing bracket of the if/else statements. The semicolon is going to halt execution and not do the else regardless of what else you have.

Also for sessions, just make sure you put session_start(); at the very top of the page (directly after the opening PHP tag and make sure there is no space or HTML before that.

Then all you have to do is set your variables:
<?php
$_SESSION['user'] = $_POST['username'];
$_SESSION['pass'] = $_POST['password'];
$_SESSION['leve'] = $_POST['level'];
?>

Then as long as you put session_start(); at the top of the rest of the pages you use, you should have access to those variables and you can show or hide different parts of your site depending on what level people are at. And if those variables aren't set, just redirect them to the login page.

Info: PHP Manual


#3 adiwood

adiwood
  • Members
  • Pip
  • Newbie
  • 2 posts

Posted 31 March 2006 - 02:31 PM

Hi,

Thank you for your reply. I have edited the code, and it still works, so that is sorted. Will looking into the sessions now.

Thank you for your reply.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users