homepage Welcome to WebmasterWorld Guest from 54.227.40.166
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Problem with setting cookies
amikin




msg:3386430
 10:26 pm on Jul 4, 2007 (gmt 0)

Hi Everyone,

I'm trying to set a cookie for a site to use to verify if a member has logged in.

I've got the cookie working fine, the problem is it only works when I have it as the first line of code. If I move it anywhere else in my php scripts, it stops setting the cookie.

Here is a sample of the code. I've put the setcookie in the spot where I need it to be, which doesn't work. If I move the setcookie to the top of the page it works.

if($submitType == "login"){
$username = $_POST['username'];
$password = $_POST['password'];

$loginStmt = "SELECT * FROM $tablename WHERE business_email = '$username' ";
$loginresult = mysql_query($loginStmt) or die("Problem with finding the login info. Please alert us and let us know you were having this problem. Our error reference is: " . mysql_error() . ". <BR><BR>The query data is: " . $loginStmt);
$num = mysql_num_rows($loginresult);
if($num > 0){
$testLogin = mysql_fetch_array($loginresult);
$mem_num = $testLogin['membership_number'];
if($mem_num == $password){
$msg = "Login Successful";
setcookie("login_successful", $testLogin['firstName'], time()+3600,"/");
} else {
$msg = "The password is incorrect.";
}
} else {
$msg = "The username was not found.";
}

} else if($submitType == "logout") {
echo "need logout script";

}
@mysql_free_result($loginresult);

 

barns101




msg:3386432
 10:32 pm on Jul 4, 2007 (gmt 0)

The cookie must be set before any output is sent to the browser - i.e. before any text is displayed. Is your script set up to display errors?

amikin




msg:3386450
 11:19 pm on Jul 4, 2007 (gmt 0)

Well, there is error reporting in the select statements.

I've also tried moving that whole block of code to the top of the page, but it's looking like the cookie needs to be the first thing to happen or it doesn't set.

Would the solution be to check the login, if login is successfull, forward to a new page that sets the cookie, then forward back to the original page?

I've seen many sites with logins, and they always seem to bounce between pages before the login successful message is displayed, is that what is happening?

barns101




msg:3387131
 6:15 pm on Jul 5, 2007 (gmt 0)

Your code looks correct. Remember that you can't read a cookie from inside the page that initially set it without either refreshing the page or moving to another, because the cookie data is sent as a header when loading a page.

mattclayb




msg:3387186
 7:35 pm on Jul 5, 2007 (gmt 0)

try this -

if($submitType == "login"){
$username = $_POST['username'];
$password = $_POST['password'];

$loginStmt = "SELECT * FROM $tablename WHERE business_email = '$username' ";
$loginresult = mysql_query($loginStmt) or die("Problem with finding the login info. Please alert us and let us know you were having this problem. Our error reference is: " . mysql_error() . ". <BR><BR>The query data is: " . $loginStmt);
$num = mysql_num_rows($loginresult);
$testLogin = mysql_fetch_array($loginresult);
$mem_num = $testLogin['membership_number'];
mysql_close();
if($num > 0 && $mem_num == $password){
$msg = "Login Successful";
setcookie("login_successful", $testLogin['firstName'], time()+3600,"/");
} else {
$msg = "The password or username is not found.";
}
}
if($submitType == "logout") {
echo "need logout script";
}
@mysql_free_result($loginresult);

natural number




msg:3389228
 4:28 am on Jul 9, 2007 (gmt 0)

As far as I know,

The set-cookie function is basically a wrapper for:
header ("set-cookie: batman = the dark knight");

Headers have to be sent before anything is output to the screen, or php will throw out an error. Therefore, setcookie() must be called before you output anything on screen, including whitespace. Therefore, I think, you should put setcookie() before print all of the or echo statements in your script.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved