Need some help with login script (logged page comes late)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Electric_Zax
Forum Newbie
Posts: 2
Joined: Tue Apr 14, 2009 7:28 am

Need some help with login script (logged page comes late)

Post by Electric_Zax »

Hi guys. I'm going to lose my virginity on this forum (this is my first time here) so be gentle, please ^.^
I have this issue with a site I work on. So I have this
<div ID="whatever"> <?php include "login.php" ?> </div>
in a page and everything seems to work normal. If I am logged in, there's a "log out" link, and if I am not logged in, there's this form to writing your UN/PW in. Everything is fine, but after I write my UN/PW and press "Sign in" it shows the form as nothing has happened. If I move to another page on the site it changes to "sign out". So basicly it works, but it doesn't change when it have to change. Can you help me out? What do I have to change (if it's something simple) to make it show "Log out" after I click on the "Sign in" button?

Here's the "login.php" code:

Code: Select all

 
<?php
 
include ("../../connect.php"); 
 
if (!$_SESSION['user'])
 
    {
    ?>
        <img src="images/userlogin.jpg" alt="Sign in" />
 
        <form method="post" action="aboutus.php">
            <table border="0">
                <tr><td><p class="caption">Name:</p></td></tr>
                <tr><td><input name="user" type="text" /></td></tr>
                <tr><td><p class="caption">Password:</p> </td></tr>
                <tr><td><input name="pass" type="password" /></td></tr>
                <tr><td style="text-align: right;"><input type="submit" name="Submit" value="Sign in" /></td></tr>
            </table>
        </form>
        <p>Or <a href="register.php">Register</a>.</p>
        <br />
        
        
    <?php 
        if ($_POST['Submit'] == 'Sign in')
            {
            if ($_POST["user"] == NULL)
                {
                echo ("<p>Empty Name field.</p>");
                exit();
 
                }
            else
                {
                if ($_POST["pass"] == NULL)
                    {
                    echo ("<p>Empty Password field.</p>");
                    exit();
 
                    }
                }
            $username = mysql_real_escape_string($_POST['user']);
            $password = md5($_POST['pass']);
            $sql = "SELECT * FROM users WHERE
                username = '$username' AND
                password = '$password'";
            $result = mysql_query($sql) or die (mysql_error());
            $num = mysql_num_rows($result);
            if ($num == 0)
                {
                echo ("<p>Invalid Name/Password</p>");
                }
            else
                {
                    $_SESSION['user'] = $username;
                    header("Location: aboutus.php");                    
                }
            }
    }
else
    {
    ?>
        <a href="logout.php">Sign out</a>
    <?php
    }
?>
 
tech603
Forum Commoner
Posts: 84
Joined: Thu Mar 19, 2009 12:27 am

Re: Need some help with login script (logged page comes late)

Post by tech603 »

Code: Select all

# else
#     {
#     ?>
#         <a href="logout.php">Sign out</a>
It looks like you have your log out link showing in the else statement. Meaning that it only shows when all else is not true, even when your login is successful. You may want to look at your conditional statements again to ensure they are doing what you want them to do.

Hope that helps.
Electric_Zax
Forum Newbie
Posts: 2
Joined: Tue Apr 14, 2009 7:28 am

Re: Need some help with login script (logged page comes late)

Post by Electric_Zax »

I checked them several times but I guess I'm just not that experienced, because I don't find anything unusual, strange, etc. I Tried switching the places of if/else

Code: Select all

 
if ($_SESSION['user'] != NULL)
    {
    .... logout.php
    }
else
    {
    ...<form>...
    }
 
I also tried instead of $_SESSION['user'] to use another variable, I also tried putting it in connect.php (I had a page that worked just fine that way, but I can't get it now and see what's the difference between these two) but it did the same thing. You can see the problem for yourself here:
http://exscapex.freehostia.com
Post Reply