By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,044 Members | 1,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,044 IT Pros & Developers. It's quick & easy.

Problem setting Cookie Please help!

P: n/a
Hi,

I am running the following code to set a cookie but it is not working,
please
help me sorting out the problem. The code is from an online article
http://www.devarticles.com/art/1/639

I am using Windows 2000 SP5, PHP 4.2.3, MYSQL 4.0.13-nt, apache 2.0.45
<?php
$psEmail = $_POST["psEmail"];
$psPassword = $_POST["psPassword"];
$psRefer = $_Post["psRefer"];

// Check if the information has been filled in
if($psEmail == '' || $psPassword == '')
{
// No login information
header('Location: LoginTest.php?refer='.urlencode($psRefer));
}
else
{
// Authenticate user
$hDB = mysql_connect('localhost', 'root', 'sarah');
mysql_select_db('TestData', $hDB);
$sQuery = "Select iUser, MD5(UNIX_TIMESTAMP() + iUser +
RAND(UNIX_TIMESTAMP())) sGUID From tblUser Where sEmail = '$psEmail'
And sPassword = password('$psPassword')";

$hResult = mysql_query($sQuery, $hDB);
if(mysql_affected_rows($hDB))
{
$aResult = mysql_fetch_row($hResult);
// Update the user record
$sQuery = "
Update tblUser
Set sGUID = '$aResult[1]'
Where iUser = $aResult[0]";
mysql_query($sQuery, $hDB);
// Set the cookie and redirect
setcookie("session_id", $aResult[1], time() + 3600)
}
else
{
// Not authenticated
header('Location: LoginTest.php?refer='.urlencode($psRefer));
}

}
?>
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
*** Sarah Michael wrote/escribió (5 Nov 2003 22:13:09 -0800):
I am running the following code to set a cookie but it is not working


I suggest to try to identify what part of your code doesn't work properly.
You're mixing form processing, database access, cookies... God knows what
fails here ;-)
--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #2

P: n/a

"Sarah Michael" <sa************@yahoo.com> wrote in message
news:2a*************************@posting.google.co m...
Hi,

I am running the following code to set a cookie but it is not working,
please
help me sorting out the problem. The code is from an online article
http://www.devarticles.com/art/1/639

I am using Windows 2000 SP5, PHP 4.2.3, MYSQL 4.0.13-nt, apache 2.0.45
[snipped]> } ?>


Are you getting any error messages (if not, are errors displayed? They could
be switched off in your php.ini file so check log files instead).

Use headers_sent() to see if you have managed to send a blank space or
something to the browser before setting the cookies (thus, in case you
didn't know - no output to the browser should occur before any headers being
sent).

Secondly - I would put an exit after your call to header("Location:"...

Sometimes I have found problems mixing functions inside a call with the
header function... thus

header('Location: LoginTest.php?refer='.urlencode($psRefer));

*maybe* a bad idea...

Try instead something like

$tmp="LoginTest.php?refer=".urlencode($psRefer);
header("Location: $tmp");
exit;

Lastly... instead of redirecting to "LoginTest.php" try inserting the full
domain path name (thus use http://www.mydomain.com/LoginTest.php instead of
just plain LoginTest.php)
While the above are not directly related to createing cookies, an error in
code preceeding the setcookie could well be the problem of characters being
sent to the browser, thus making setcookie fail (since only headers/cookies
should be sent first).

If the above doesn't help you - then tell us what error messages you are
getting...
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.