[SOLVED] simple login problem

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
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

[SOLVED] simple login problem

Post by rsmarsha »

I'm adding a simple login script for a few pages and even though i've done this before, for some reason it's not holding the session from page to page.

The code for the index is :

Code: Select all

 
<?php
session_start();
echo 'SID '.session_id();
include 'boardroom/db_conn.php';
include '/var/www/html/functions/security.php';
if ($_GET['logout']=='1')
{
    $_SESSION = array();
    session_destroy();
}
if (isset($_POST['email']) && isset($_POST['password']))
{
    $email = makeSafe($_POST['email']);
    $password = makeSafe($_POST['password']);
    if ((strlen($email)>1) && (strlen($password)>1))
    {
        // if the user has just tried to log in
        $password = sha1($password);
        $login = "SELECT email,name FROM users WHERE email='".$email."' AND password='".$password."'";
        $lq = mysql_query($login) or die("Query $login Failed".mysql_error());
        $lr = mysql_fetch_assoc($lq);
        if (mysql_num_rows($lq) >0)
        {
            $_SESSION['email'] = $email;
            $_SESSION['name'] = $lr['name'];
        }
    }
}
?>
<html>
<head>
<title></title>
</head>
<body>
<h3></h3>
<br />
<?php if(isset($_SESSION['email']))
{
    echo 'Welcome to football '.$_SESSION['name'].'.';
    echo '<br />';
    echo '<p><a href="index.php?logout=1">Logout</a></p>';
    echo '<p><a href="predictions.php">Your Predictions</a></p>';
}
else
{
    if ($_GET['message']=='thankyou')
    {
        echo '<p>Thank you for registering for football, you will receive an email with a reminder of your login information.</p>';
        $loginText = 'P';
    }
    else
    {
        $loginText = 'If you are already registered with us p';
    ?>
<a href="register.php" />New User? Click to Register</a>
<br /><br />
<?php
    }
    ?>
<form method="post" action="index.php">
<table>
<tr>
<td colspan="2">
<?php echo $loginText; ?>lease login using the form below.
</td>
</tr>
<tr>
<td>Username</td>
<td><input type="text" name="email" id="email"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" id="password">
</tr>
<tr>
<td></td>
<td><input type="submit" name="login" id="login" value="Login"></td>
</tr>
</table>
</form>
<?php
}
    ?>
    
</body>
</html>
 
The link to predictions should allow you to stay logged in but doesn't, also if click on the url bar in the browser and hit enter it logs me out also.

predictions.php

Code: Select all

 
<?php
session_start();
echo 'SID '.session_id();
print_r($_SESSION);
if (isset($_SESSION['email']))
{
    echo 'Logged in as '.$_SESSION['name'];
}
else
{
    echo 'Not logged in';
}
?>
 
Any help much appreciated, it's probably something simple i'm missing.
Last edited by rsmarsha on Fri May 02, 2008 7:42 am, edited 1 time in total.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: simple login problem

Post by aceconcepts »

Always make sure there is no whitespace i.e. line 1
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Re: simple login problem

Post by rsmarsha »

There isn't any, must just be the way I posted it. :)
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Re: simple login problem

Post by rsmarsha »

I've even tried another php login script and it seems I have the same issue with that.
Last edited by rsmarsha on Fri May 02, 2008 7:42 am, edited 1 time in total.
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Re: simple login problem

Post by rsmarsha »

An update. It seems that it's only in firefox 3 beta 5. Firefox 2 and IE work fine. Any idea why this could be?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: simple login problem

Post by aceconcepts »

I just tested it in Firefox 2 and it works fine for me.

Have you checked the browser settings for beta 3?
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Re: simple login problem

Post by rsmarsha »

Looking at them now, can't spot anything as of yet.
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Re: simple login problem

Post by rsmarsha »

Solved it. :)

For some reason when you click page info under tools it was set to block cookies for the site under permissions. :)
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: simple login problem

Post by aceconcepts »

Well done.
Post Reply