Forum Moderators: coopster
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
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
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
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
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>";