php cookie not getting set from cookie newbie
| From: | Stuart McDonald | Date: | Wed, 30 Aug 2000 01:38:51 +0000 |
| Subject: | php cookie not getting set from cookie newbie | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-2466@lists.php.net to get a copy of this message | ||
Hi,
I am trying to teach myself about cookies and using them for user
authentication. I got the following code from the Wrox (Professional PHP
Programmig) text (which is great) but cannot get it to set the cookie - any
suggestions would be much appreciated. (Cookies is turned on as I can get it
to work with a simple count cookie)
This is the login file (login.php3 that the login form calls):
___start of login.php3____
<?php
require 'includes/functions.php3';
if(authenticateUser($form_user_id, $form_password)){
setcookie("user",$form_user_id);
setcookie("passwd",$form_password);
header("Location:http://$HTTP_HOST/$doc_root/modify/modify_sam.php3");
exit();
}else{
header("Location:http://$HTTP_HOST/$doc_root/modify/error1.php3");
exit();
}
?>
___end____
and this is the functions file that is referenced above:
___start of functions.php3____
<?php
require 'includes/common.php3';
//function authenticate user
function authenticateUser($user, $password)
{
global $db_server,$HTTP_HOST,$db_login,$db_password,$db_name,$doc_root;
//open a persistent connection with the MySQL server
if(!($link=mysql_pconnect ($db_server,$db_login,$db_password))){
DisplayErrMsg(sprintf("internal error
%d:%s\n",mysql_errno(),mysql_error()));
return 0;
}
//do the password/user authentication
if(!($result=mysql_db_query("$db_name", "SELECT * FROM user_profile WHERE
user_id='$user'"))){
DisplayErrMsg(sprintf("internal error
%d:%s\n",mysql_errno(),mysql_error()));
return 0;
}
//compare results
if(($row=mysql_fetch_array($result)) && ($password == $row["password"] &&
$password != " "))
return 1;
else
return 0;
}
___end___
the common.php3 (referenced above) just has the db stuff in it, that you
don't need to see.
The authenticate part works in that it will only let people in that have
used the correct sign in and password, but it is not setting a cookie
(browser does not alert me), and then when they get to the edit page it will
not authenticate them again but rather lets any one in.
the first page of the edit screen ie., after being authentcated (but without
the HTML that follows after the PHP), is:
____start of modify_sam.php3____
<?php
require 'includes/functions.php3';
//check for authentication
if(!authenticateUser($cookie_user, $cookie_passwd)){
header("Location:http://$HTTP_HOST/$doc_root/modify/login.html");
exit();
}
//Connect to the database
if(!($link=mysql_pconnect($db_server,$db_login,$db_password))){
DisplayErrMsg(sprintf("internal error
%d:%s\n",mysql_errno(),mysql_error()));
exit();
}
?>
_____end____
So everytime a user trys to login the get a "bad login screen" if the
incorrect login is used (correct) but if the correct one is used, they end
up back at the login screen as they are not authenticated when they reach
modify_sam.php3 because the cookie has not been set.
Any suggestions on where I am going astray here, or some good web references
would be much appreciated.
Cheers
Stuart