Forum Moderators: coopster

Message Too Old, No Replies

user login page

         

sfast

8:55 pm on Mar 31, 2007 (gmt 0)

10+ Year Member



I am not able to figure out where the problem lies for this code. When I input wrong id, it does not show me any error and
when I put the correct user name and password, it does not go to main.php.

Can somebody point to the error here please?

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

if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {

include 'connect.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) {

$_SESSION['db_is_logged_in'] = true;
// after login we move to the main page
header('Location: main.php');
exit;
} else {
$errorMessage = 'Sorry, wrong user id or password';
}

include 'library/closedb.php';
}
?>

sfast

9:05 pm on Mar 31, 2007 (gmt 0)

10+ Year Member



Sorry I meant to say that it does not show me any error or does not go to main.php when user name and password match.

sfast

7:31 am on Apr 1, 2007 (gmt 0)

10+ Year Member



ok I could solve one problem but I still have anoher one -

if (!isset($_SESSION['user_logged'])
¦¦ $_SESSION['user_logged']!= '123') {

it does n't take any value on main.php.

The loop goes to not logged in, move to the login page

Please please help me. This is drving me crazy.
What is wrong here.

coopster

4:50 pm on Apr 2, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Regarding your header() [php.net] call in the first message:

Note: HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:

Just a heads up.

In regards to the new issue, are you starting the session in main.php? You must do that first.

sfast

5:52 pm on Apr 2, 2007 (gmt 0)

10+ Year Member




if (mysql_num_rows($result) == 1) {

$_SESSION['user_logged'] = true;

// after login we move to the main page


$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'main.php';
header("Location: [$host$uri...]
exit;
}

else { $errorMessage = 'Sorry, wrong user id or password';
echo $errorMessage ; }

I changed the above location. But no success.
It still stays on the same page - login.php

sfast

5:53 pm on Apr 2, 2007 (gmt 0)

10+ Year Member



And yes, I am starting the session as first line in main.php

eelixduppy

7:17 pm on Apr 2, 2007 (gmt 0)



Turn up error_reporting [php.net] to see if anything comes up. Also, make sure that you have the correct username and password, and that your password is properly encrypted, etc... Also, try to use a static URI for testing purposes, just to get it to work (just in case ;)):

header("Location: http://example.com/logged_in/");

We also have a great thread in the PHP Forum Library on User Authentication [webmasterworld.com]. It's always a good reference :)

sfast

5:26 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



I changed the code and I ran it on localhost on my PC for testing. header("Location: http://example.com/logged_in/"); works with my PC.

But when I try to run this on hosting site, it doesn't take it. I have tried several different ways to redirect. But nothing works.

Does it have to do with the settings of php.ini file?