Login twice
Moderator: General Moderators
Login twice
hey everybody-
A while ago, i read a post from someone who developed a login script, and for some reason, users had to login twice just to make it work. The after searching for the post, I found it had no solution, so I thought i'd see if anyone else was experiencing this problem becasue it just happened to me.
Basically, I login successfully, start a session, then redirect to page 2.
On page 2, I have the typical, "if session doesn't exist, redirect to login screen" script on there. Even though I just logged in successfully, it still redirects me to the login screen. then, without refreshing the page, i'll just login again using the same username and password, and everything works fine.
Any ideas? Thanks for the help/opinions on why this happens.
A while ago, i read a post from someone who developed a login script, and for some reason, users had to login twice just to make it work. The after searching for the post, I found it had no solution, so I thought i'd see if anyone else was experiencing this problem becasue it just happened to me.
Basically, I login successfully, start a session, then redirect to page 2.
On page 2, I have the typical, "if session doesn't exist, redirect to login screen" script on there. Even though I just logged in successfully, it still redirects me to the login screen. then, without refreshing the page, i'll just login again using the same username and password, and everything works fine.
Any ideas? Thanks for the help/opinions on why this happens.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
thanks for the response.
1. I've tried it on multiple browsers (firefox, ie, safari...), and they all give the same problem
i've also tried setting up the "if session doesn't exist" message to either write out "error" or a second option of the redirecting to the login screen. the printing of the word "error" allows the user to login twice and ends in a successfull entry, however the redirect method, just keeps going back to the login screen no matter how many times you login.
login script:
And here's my page2.php:
1. I've tried it on multiple browsers (firefox, ie, safari...), and they all give the same problem
i've also tried setting up the "if session doesn't exist" message to either write out "error" or a second option of the redirecting to the login screen. the printing of the word "error" allows the user to login twice and ends in a successfull entry, however the redirect method, just keeps going back to the login screen no matter how many times you login.
login script:
Code: Select all
<?PHP
connect db stuff...
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM usertable WHERE username='".$username."' and password='".$password."'";
$result = mysql_query($sql) or die ("Couldn't get reults.");
$count = mysql_num_rows($result);
if ($count == 1 ) {
session_start();
$_SESSION['username'] = "success";
header( "Location: http://www.example.com/page2.php" );
}
else if ($count == 0) {
header( "Location: http://www.example.com/login.html" );
}
?>Code: Select all
<?php
session_start();
header("Cache-control: private");
if ( !isset( $_SESSION['username'] ) ) {
echo "error" ;
//header( "Location: http://www.example.com/login.html" );
exit;
}
rest of web page here..
?>ok, I read up on the session_write_close() function, and it seems to make sense, however I've got the same problem. I set the session_write_close() on the login script like so:
There's no reason to put the session_write_close(); on my page2.php right?
Thanks again.
Code: Select all
<?PHP
connect and sql stuff...
if ($count == 1 ) {
session_start();
$_SESSION['username'] = "success";
session_write_close();
header( "Location: http://www.example.com/page2.php" );
}
else if ($count == 0) {
header( "Location: http://www.example.com/login.html" );
}
?>Thanks again.