Help With Cookies!!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Darksevear
Forum Newbie
Posts: 11
Joined: Sat May 13, 2006 10:38 pm

Help With Cookies!!!!

Post by Darksevear »

I need some help with these two PHP Scripts. I dont know where to put the cookies.

I need to create a cookie once the user has logged in. In the cookie i want to just have the $user once the user has logged in. Please Help. Which Script do i put in the cookie? and how? could you please give me an example?

Login.php:

Code: Select all

<?php 

if (isset($_POST['submit'])) {  // check to see if the forum has been submitted 
    //  where is tasys 'submit', use the name of the submit button on the form 
   include('connect.php'); 


   // get form input 
    // check to make sure it's all there 
    // escape input values for greater safety 
    $user = empty($_POST['user']) ? die ("ERROR: Enter a Username") : mysql_escape_string($_POST['user']); 
    $pass = empty($_POST['pass']) ? die ("ERROR: Enter a Password") : mysql_escape_string($_POST['pass']); 

   $sql="SELECT * FROM users WHERE user='$user' and pass='$pass'"; 
   $result=mysql_query($sql); 

   // Mysql_num_row is counting table ro 

   $count=mysql_num_rows($result); 

   // If result matched $user and $pass, table row must be 1 row 

   if($count==1){ 

      // Register $user, $pass and redirect to file "login_success.php" and make cookie that saves the $user variable
	  //will add redirect script soon.
	  

   } 
   else { 
      echo "Wrong Username or Password"; 
   } 
} 

?> 

<html> 
<head> 
</head> 

<body> 

<?php 
if (!isset($_POST['submit'])) { 
// form not submitted 
?> 

    <form action="<?=$_SERVER['../../Documents%20and%20Settings/Drezard/My%20Documents/My%20Received%20Files/PHP_SELF']?>" method="post"> 
    Username: <input type="text" name="user"> 
    Password: <input type="text" name="pass"> 
    <input type="submit" name="submit"> 
    </form> 

<?php 
} 
?> 
</body> 
</html>

login_success.php:

Code: Select all

<?php
//create a cookie with $user from login.php
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<?php
echo " congratz you are logged in.";
if (isset($_COOKIE["user"])) {
echo "hi " $user;
}
else { 
echo "You are not logged in!<br />";
}
?>

</body>
</html>
Please help cheers, Daniel
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

My suggestion, as soon as you know the user is authenticated, set your cookie. Regardless of where that point is in your code, that is where I usually set all my 'user_' type cookies.
Post Reply