Jump to content

Archived

This topic is now archived and is closed to further replies.

adiwood

Sessions help!

Recommended Posts

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:



[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);


}
?>[/code]

Share this post


Link to post
Share on other sites
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:
[code]<?php
$_SESSION['user'] = $_POST['username'];
$_SESSION['pass'] = $_POST['password'];
$_SESSION['leve'] = $_POST['level'];
?>[/code]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.