...

View Full Version : Problem with logging in code



wincode
11-12-2011, 11:00 PM
Hi I'm having problems with a login code. The problem is that the first time I login, then go to a different page, it logs me out. But on the second login, this problem doesn't happen.
I use a page login.php to process login:

<?php
mysql connection stuff
session_start();
$user=$_POST['user'];
$pass=$_POST['pass'];
$loginchek=mysql_query("SELECT * FROM users where user='$user'") or die(mysql_error());
$loginchek2=mysql_fetch_array($loginchek);
if($loginchek2['pass']==$pass){

$_SESSION['user']=$loginchek2['user'];

$_SESSION['userid']=$loginchek2['id'];
header('Location: index.php');
echo"You are now logged in!";

}
else{
echo"Wrong password";
}

?>

What could be causing the problem?

Thanks

Adee
11-13-2011, 12:16 AM
Hi I'm having problems with a login code. The problem is that the first time I login, then go to a different page, it logs me out. But on the second login, this problem doesn't happen.
I use a page login.php to process login:

<?php
mysql connection stuff
session_start();
$user=$_POST['user'];
$pass=$_POST['pass'];
$loginchek=mysql_query("SELECT * FROM users where user='$user'") or die(mysql_error());
$loginchek2=mysql_fetch_array($loginchek);
if($loginchek2['pass']==$pass){

$_SESSION['user']=$loginchek2['user'];

$_SESSION['userid']=$loginchek2['id'];
header('Location: index.php');
echo"You are now logged in!";

}
else{
echo"Wrong password";
}

?>

What could be causing the problem?

Thanks

1) encase ur login variables with mysql_escape_string(); function
2) u need session_start(); function on all pages
3) if ur going to just use the username as the session why not just use $_SESSION['username'] = $_POST['username'] ?

also when you check for user credentials you want to do SELECT username, userid FROM users WHERE username='$username' AND password='$password'

mlseim
11-13-2011, 12:20 AM
Do you have this at the top of every other page too? (you should have it).

<?php
mysql connection stuff
session_start();


Also,
Change this line:
echo"You are now logged in!";
To:
exit;

You're redirecting, so that line should not be there.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum