...

View Full Version : cookies



robojob
09-16-2005, 08:32 PM
hi, i have this log in script:


<?php


include 'config.php';

ob_start();
echo "<font size=\"2\" face=\"verdana\"> Login </font><br>";
echo "<form action=\"./login.php\" method=\"POST\">";
echo "<font size=\"1\" face=\"verdana\">Name:</font> <br><input type=\"text\" name=\"username\"><br>";
echo "<font size=\"1\" face=\"verdana\">Your email:</font> <br><input type=\"text\" name=\"email\"><br>";
echo "<font size=\"1\" face=\"verdana\">Pass: </font><br><input type=\"password\" name=\"password\"><br>";
echo "<input type=\"submit\" value=\"Login!\">";
echo "</form>";
echo "<font size=\"1\" face=\"verdana\">Forgot your password?<br> Click <a href=\"reset.php\">here!</font></a>";

$connection = @mysql_connect($hostname, $user, $pass)
or die(mysql_error());
$dbs = @mysql_select_db($database, $connection) or
die(mysql_error());

$sql = "SELECT * FROM `users` WHERE username = '$_POST[username]' AND password = '$_POST[password]' AND email = '$_POST[email]'";
$result = @mysql_query($sql,$connection) or die(mysql_error());
$num = @mysql_num_rows($result);

if ($num != 0) {
$cookie_name = "auth";
$cookie_value = "fook";
$cookie_expire = "0";
$cookie_domain = $domain;

setcookie($cookie_name, $cookie_value, $cookie_expire, "/", $cookie_domain, 0);
header ("Location: http://" . $domain . $directory . "index1.php");

ob_end_flush();

exit;
}
?>

can someone help me to add the username of the user to the cookie that is created and then advise me of the code i can use on another page to retrive the username set in the cookie to enable me to query the database...

raf
09-16-2005, 08:47 PM
i'm a bit confused.

do you wanna replace the "fook" by the username? or does that values needs to be kept as well?

robojob
09-16-2005, 08:52 PM
that value can be changed to the username. how would i do that and how would i get that from my page that you see when logged in?

robojob
09-16-2005, 08:55 PM
the top of my protected page looks like this if that helps: (think the fook might need to stay...


<?php
include 'config.php';
$tmp = $_GET['action'];
if($tmp == "signout"){
$cookie_name = "auth";
$cookie_value = "";
$cookie_expire = "0";
$cookie_domain = $domain;
setcookie($cookie_name, $cookie_value, $cookie_expire, "/", $cookie_domain, 0);
header ("Location: http://" . $domain . $directory . "index.php");
}

if($_COOKIE['auth'] == "fook"){
echo "";

echo ;
}else{
header ("Location: http://" . $domain . $directory . "index.php");
exit;
}
?>

gunman
09-16-2005, 09:25 PM
Just put username in cookie and after that you can check whether that cookie exist.

robojob
09-17-2005, 09:00 AM
that made no sense to me what so ever. can someone explain what i need to do. i am really new to php so not exactly sure what im doing.!

raf
09-17-2005, 08:58 PM
well, if that 'fook' needs to remain the value of cookie 'auth', then i think it would be best to just set a second cookie.
(you can use one cookie as an array of values, but that would still mean you need to check and maybe adjust your other pages that are now using that cookie.)

so, to set the extra cookie, it would be as simple as


//your existing cookie being set
setcookie($cookie_name, $cookie_value, $cookie_expire, "/", $cookie_domain, 0);
//set extra cookie
setcookie('uname', $_POST[username], $cookie_expire, "/", $cookie_domain, 0);

and to read the value on other pages, it's just


if (isset($_COOKIE['uname'])){
$username = $_COOKIE['uname'];
}

robojob
09-17-2005, 10:20 PM
hi raf,

i have added in the code for the login page creating the cookie and then i have added in this line of code into the page that i want to use the cookie info:



if (isset($_COOKIE['username'])){
$username = $_COOKIE['username'];
$query = "SELECT * FROM users WHERE username='" . mysql_real_escape_string($_COOKIE['username']) . "'";
$result = mysql_query($query) or die("Mysql Error: ".mysql_error);

while($row = mysql_fetch_array($result)){

?>


This doesnt appear to work! any ideas?

The Doc
09-17-2005, 10:26 PM
is the Cookie set?
try that:


echo '<pre>';
var_dump($_COOKIE);
echo '</pre>';

and: don't forget to reload the page after setting the cookie - only than you'll be able to see if it is set or not.

robojob
09-17-2005, 10:30 PM
am i meant to be placing that in a single php file with no other code?

raf
09-17-2005, 10:30 PM
"This doesnt appear to work!" as not much to go on ...

i do however see that you have
if (isset($_COOKIE['username'])){

--> did you rename the cookie in the code i posted to set it ?

also, inside your query, use $username instead of $_COOKIE['username']

if it doesn't work, then add in some echo's at strategical points. Like


if (isset($_COOKIE['username'])){
echo '1<br />';
$username = $_COOKIE['username'];
echo $username;
$query = "SELECT * FROM users WHERE username='" . mysql_real_escape_string($_COOKIE['username']) . "'";
echo '<br />', $query;
$result = mysql_query($query) or die("Mysql Error: ".mysql_error);
echo '2<br />';
while($row = mysql_fetch_array($result)){

and then look at the results to follow the executionpath.

The Doc
09-17-2005, 10:32 PM
am i meant to be placing that in a single php file with no other code?

;) that code is meant to be placed inside your existing php-file. I would say just before the if(isset())

robojob
09-17-2005, 10:38 PM
k, its working now! i noticed a mistake that i made somewhere else in the code!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum