...

View Full Version : PHP login redirect error



Nuttz77
02-23-2007, 02:58 PM
Hey all,

Having trouble with a php login. The login process itself is not failing. I have a tbl_auth_user with the login info inside my database and it views it and authenticates perfectly, thanks to somebodies words of wisdom here. The issue I have is on successful login it goes to a blank screen and still looks as if its on the login page (still shows xxx.xxx/englogin.php). If anyone can let me know what I did wrong I would appreciate it!! :confused: Here is the page script::

<?php
// we must never forget to start the session
session_start();

$errorMessage = '';
if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {
include 'opendb.php';
include 'config.php';

$userId = $_POST['txtUserId'];
$password = $_POST['txtPassword'];

// check if the user id and password combination exist in database
$sql = "SELECT user_id
FROM tbl_auth_user
WHERE user_id = '$userId' AND user_password = PASSWORD('$password')";

$result = mysql_query($sql) or die('Query failed. ' . mysql_error());

if (mysql_num_rows($result) == 1) {
// the user id and password match,
// set the session
$_SESSION['db_is_logged_in'] = true;

// after login we move to the main page
header('Location: enggui.html');
exit;
} else {
$errorMessage = 'Sorry, wrong user id / password';
}

include 'closedb.php';
}
?>
<html>
<head>
<title>Engineering Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<center><img src="ABC_FOX Logos.JPG" width="156" height="44"></center>
<body>

<?php
if ($errorMessage != '') {
?>
<p align="center"><strong><font color="#990000">
<?php echo $errorMessage; ?></font></strong></p>

<div align="center">
<?php
}
?>
</div>
<form action="" method="post" name="frmLogin" id="frmLogin">
<table width="400" border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<td width="150">User Id</td>
<td><input name="txtUserId" type="text" id="txtUserId"></td>
</tr>
<tr>
<td width="150">Password</td>
<td><input name="txtPassword" type="password" id="txtPassword"></td>
</tr>
<tr>
<td width="150">&nbsp;</td>
<td><input name="btnLogin" type="submit" id="btnLogin" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>

CFMaBiSmAd
02-23-2007, 03:25 PM
There are two possibilities. The header is not being sent or the location URL is not the correct format or does not exist.

For the first possibility, add the following lines after your first opening <?php tag -

ini_set ("display_errors", "1");
error_reporting(E_ALL);For the second possibility, the URL in a header redirect technically needs to be a full/absolute URL (relative URL's work depending on browser type and version) -

header('Location: http://yourdomain.com/enggui.html');

Nuttz77
02-23-2007, 05:01 PM
OK, so after entering your script and adjusting the destination of enggui.html to read the full http://mydomain.com/enggui.html, I get the following error when initially bringing up the login page:

Warning: Unknown(): open(/var/lib/php/session/sess_de79ce8275ea6187eafa8c47e26188a2, O_RDWR) failed: Permission denied (13) in Unknown on line 0

Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php/session) in Unknown on line 0

Once I enter a valid user, I get redirected to the blank page where I see the following error:

Query failed. No database selected
Warning: Unknown(): open(/var/lib/php/session/sess_de79ce8275ea6187eafa8c47e26188a2, O_RDWR) failed: Permission denied (13) in Unknown on line 0

Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php/session) in Unknown on line 0

Nuttz77
03-02-2007, 09:00 PM
So I managed to get rid of all those errors, and now when i enter a valid user, I go to a blank page that says, Query failed. No database selected. Thats it, no redirect to the enggui.html and I am not sure why its asking for a database cause it should just be going to this page where parts can be looked up.

I am not very good at this php as you can all see by now. Thanks though for any assistance and for previous assistance as well.

Inigoesdr
03-02-2007, 09:23 PM
Check opendb.php & config.php and make sure your database information is correct, in particular mysql_select_db() and the $variable it uses(if any).

Nuttz77
03-02-2007, 10:06 PM
Check opendb.php & config.php and make sure your database information is correct, in particular mysql_select_db() and the $variable it uses(if any).



OK, so here is my opend:

<start>
$dbhost = 'localhost';
$dbuser = 'fakeuser';
$dbpass = 'password';
$dbname = 'the dbname';
mysql_select_db($dbname);
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
<end opend>

And my config:

<start>
$dbhost = 'localhost';
$dbuser = 'fakeuser';
$dbpass = 'password';
$dbname = 'the dbname';
<end config>

I feel as though I am missing something somewhere but am not very adept at this yet.

Nightfire
03-02-2007, 10:13 PM
You have to connect to the db before you select it


$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname,$conn);

Nuttz77
03-02-2007, 10:37 PM
Well that didn't seem to do anything either. I am wondering if I shouldn't just re-evaluate my needs and do something a little different. Thanks all for the help though.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum