Forum Moderators: coopster

Message Too Old, No Replies

Help with Login, Sessions & header

         

dwighty

9:48 am on Mar 12, 2006 (gmt 0)

10+ Year Member



Hey guys,

been working on the below script but can not seem to get th page to redirect to index2.php even though the user & pass are fine. Not sure what im not doing here.

****************CODE for index.php***********
// Start Session - This is the first line of the code
session_start();

/* Include required files... */

// Include the Login Coding
db_connect(); // Connect to the DB

// Define the Variable
$username = $_POST['username'];
$password = $_POST['password'];

if($_POST['submit']){

// Select all from DB Table
$query = "select username, password, first_name, last_name, user_group from _USERTABLE_ WHERE username='$username' and password='$password'";
// Execute the Query
$result = mysql_query($query) or die ("Error in Query: $query. ".mysql_error());
if (mysql_num_rows($result) > 0) {
while(list($username, $password, $first_name, $last_name, $user_group) = mysql_fetch_row($result)) {

//Unset the previous Sessions
unset($_SESSION['admin']);
unset($_SESSION['username']);
unset($_SESSION['first_name']);
unset($_SESSION['last_name']);

// Set the Sessions
$_SESSION['admin'] = $usergroup;
$_SESSION['username'] = $username;
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;

header('Location:index2.php');

}
}

if (!mysql_num_rows($result) > 0) {
echo "No good";
}
}

**************CODE for Index2.php***************
if(!$_SESSION['admin']){ redirect('index.php'); }
// Else user is valid

//The i want t display the following line.
Logged in as [<strong><? $_SESSION['username']?></strong>]

Any help would be appreciated.

Thanks

henry0

12:26 pm on Mar 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I do not have a solution however I am doing research
on a similar problem.
Not sure why but it seems (at least in my own problem)
that session persistence is lost when using "Header"

henry0

1:15 pm on Mar 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I fixed it, I had a session registered problem.

Do you have from the top of the other page a proper:

session_start();
and declare your session as follow:
$username=$_SESSION['username'];

Birdman

1:27 pm on Mar 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Like Henry0 said, you need to run session_start() at the begining of index2.php. Then the session variables will be available.

Also, you have other errors in the code, albeit not fatal errors. Try adding this(temporarily) to the top of your script:

****************CODE for index.php***********
error_reporting('E_ALL'); //show all erors and warnings
// Start Session - This is the first line of the code
session_start();
...
...

Run that and you'll see the other errors. If you cannot fix them, just check back here and we'll help ya.

Cheers

dwighty

5:46 pm on Mar 12, 2006 (gmt 0)

10+ Year Member



RIght,

I have added session_start(); to index2.php

The problem i am getting is that when the correct details are used the page will NOT re-direct to index2 in the first place.

I has also added the line
error_reporting('E_ALL'); //show all erors and warnings

but this is not displaying anything additional. What is wrong with my orginal script?

Thanks

henry0

6:27 pm on Mar 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



remove the single quotes from E_ALL

echo Username and password
and report if the values pass OK

also you have all bunch of " } "

are they really matching an opened " { "

what is this:
<<<
from _USERTABLE_
>>
did you make it up to hide the real name?
because underscores will throw an error
a table name could be compounded with underscore
but I doubt that a table could start with underscore

dwighty

8:37 am on Mar 13, 2006 (gmt 0)

10+ Year Member



Henry,

_USERTABLE_ is just a cover up, not using ' _ ' in the query.

Will try removing the ' ' and echo the username & password. Did try echoing the username and password before which as fine, then tried to redirect once username&password are correct which is where the problem occurs.

henry0

11:57 am on Mar 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have the feeling that sessions are not properly declared
Change
<<<
// Define the Variable
$username = $_POST['username'];
$password = $_POST['password'];
>>>

instead use:
$username = $_POST['username'];
$password = $_POST['password'];

$_SESSION['username'] = $username;
$_SESSION['password']=$password;

$username=$_SESSION['username']; echo"UN.$username.<br>";
$password=$_SESSION['password']; echo"PW.$password.<br>";

dwighty

1:54 pm on Mar 13, 2006 (gmt 0)

10+ Year Member



Thanks for that, I shall give it a go a bit later today and let you know how it goes.

Thanks