Jump to content


Photo

PHP Cookies


  • Please log in to reply
13 replies to this topic

#1 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 21 May 2006 - 07:17 AM

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:

<?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:

<?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

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 May 2006 - 07:28 AM

you will want to create the cookie upon login (in the login_success.php), as you wouldn't have the info you want to store in your cookie until after the user has been validated/logged in. now, depending on how you wish to use the cookie...

do you want them to be auto-logged in? then you will want to check for the cookie in the login script. but that will involved the password too, not just the user.

i don't really see any other point in only storing the user's name though... once they are logged in.. well, you already have their info from the login form and the query, so no point in cookie for that...
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 21 May 2006 - 07:39 AM

I want to put a cookie in one of those two places i just dont know how. But with Login_success.php i want the variable $user from the last page. Can u show me how please.

- Cheers, Daniel

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 May 2006 - 08:17 AM

the how is the easy part. but you have to figure out what you want a cookie for. what do you want to use the cookie for? this will determine which page you put it on.

as far as getting $user from login to login_success, put:

session_start();

at the very top of your script, right under <?php on both of your pages. then in here, you will want to add:

 $count=mysql_num_rows($result);

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

if($count==1){

$_SESSION['userinfo'] = mysql_fetch_array($result);

header('Location: login_success.php'); exit;


}

and since you have session_start(); in your login_success.php as well, you can access the user information with $_SESSION['userinfo']
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 21 May 2006 - 09:09 AM

Now my scripts look like this:

Login.php

<?php
$count=mysql_num_rows($result);
// If result matched $user and $pass, table row must be 1 row
if($count==1){
$_SESSION['userinfo'] = mysql_fetch_array($result);
header('Location: login_success.php'); exit;

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 to save user data
                echo "<meta http-equiv='refresh' content='0;url=login_sucess.php>";

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

?>

<html>
<head>
</head>

<body>

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

    <form action="<?=$_SERVER['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>

It keeps giving me an error on line 62. Thats the </html> whats wrong with that?

Login_sucess.php

<?php
 $count=mysql_num_rows($result);
// If result matched $user and $pass, table row must be 1 row
if($count==1){
$_SESSION['userinfo'] = mysql_fetch_array($result);
setcookie('user', $user, time()+36000*24*365);
if (isset($_GET[$user]))
{
    $user = $_GET[$user];
    }
setcookie('user', $user, time()+36000*24*365);
?>
<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;
}
if (!isset($_COOKIE["user"])) {
echo "You are not logged in!<br />";
}
?>

</body>
</html>

R These both right?

- Thanks, Daniel

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 May 2006 - 09:19 AM

uh..no... reread my last post. you were supposed to put

session_start();

at the beginning of each file, not that other stuff.

then you were supposed replace

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

with this:

$_SESSION['userinfo'] = mysql_fetch_array($result);
header('Location: login_success.php'); exit;


inside your if statement.

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 21 May 2006 - 09:41 AM

it says headers already sent on line 27. In the login.php. Please Help.

Thanks for all you help.

Cheers, daniel

#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 May 2006 - 10:16 AM

the header(...); will only work if you do not have any html output before it. this includes blank lines about your <?php tag. also, repost both your scripts again (and please use the code tags).
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#9 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 21 May 2006 - 11:14 AM

it gives me an error on line 19 now in login.php. Dont i need to just redirect it to login_sucess.php?

Oh well heres the script:

Login.php:
<?php
session_start();
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 rows
   $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 to save user data
$_SESSION['userinfo'] = mysql_fetch_array($result);
header('Location: login_success.php'); exit;


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

?>

<html>
<head>
</head>

<body>

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

    <form action="<?=$_SERVER['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:
<?php
session_start();
setcookie('user', $user, time()+36000*24*365);
if (isset($_GET[$user]))
{
    $user = $_GET[$user];
    }
setcookie('user', $user, time()+36000*24*365);
?>
<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";
}
if (!isset($_COOKIE["user"])) {
echo "You are not logged in!<br />";
}
?>

</body>
</html>

- Cheers, Daniel

Please Help

#10 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 May 2006 - 03:34 PM

what does the error say? since line 19 seems to be the header(..) earlier i noticed you spelled your login success file like so:

Login_sucess.php

well the header calls

login_success.php

notice the different spelling. also, are login and login_success in the same folder? if not then you will need to include the path of the login success file in the header.

<edited the wrong post. nothing was changed>
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#11 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 22 May 2006 - 06:21 AM

The scripts are both in the same file and are called "login.php" and "login_sucess.php".

Do I have to set it as a header or can i just create a redirect file?

- Cheers, Daniel

#12 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 22 May 2006 - 10:34 PM

I just want it to create a cookie with the user's details once the user has logged in. Also if the user finds the cookie on his/her harddrive, they wont be able to edit who they have logged in as.

Thanks, Daniel

- Cheers, Daniel

#13 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 May 2006 - 11:45 PM

[!--quoteo(post=375975:date=May 22 2006, 01:21 AM:name=Drezard)--][div class=\'quotetop\']QUOTE(Drezard @ May 22 2006, 01:21 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
The scripts are both in the same file and are called "login.php" and "login_sucess.php".

Do I have to set it as a header or can i just create a redirect file?

- Cheers, Daniel
[/quote]
okay you said here that it's called login_su[!--coloro:red--][span style=\"color:red\"][!--/coloro--]c[!--colorc--][/span][!--/colorc--]ess.php with one c but in your code up above the header calls login_su[!--coloro:red--][span style=\"color:red\"][!--/coloro--]cc[!--colorc--][/span][!--/colorc--]ess.php with two c's

also here:

session_start();
setcookie('user', $user, time()+36000*24*365);
if (isset($_GET[$user]))
{
    $user = $_GET[$user];
    }
setcookie('user', $user, time()+36000*24*365);
you are trying to set a cookie named 'user' with value $user but that variable doesn't exist on this page. i think what you meant to do was like,

session_start();
if ($_SESSION['userinfo']) {
   $userinfo = $_SESSION['userinfo'];
   setcookie('user', $userinfo['user'], time()+36000*24*365);
}
//what is the following code for???
if (isset($_GET[$user]))
{
    $user = $_GET[$user];
    }
setcookie('user', $user, time()+36000*24*365);
also the code after [!--coloro:red--][span style=\"color:red\"][!--/coloro--]//what is the following code for???[!--colorc--][/span][!--/colorc--] :

take it all out. take this out:

if (isset($_GET[$user]))
{
    $user = $_GET[$user];
    }
setcookie('user', $user, time()+36000*24*365);

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#14 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 23 May 2006 - 12:03 AM

(sorry for double post mods; i thought it would auto ammend previous post but too late)

continued from above:

....but that won't show your message when you check to see if the cookie is set, the first time around. your entire login_success.php (or login_sucess.php) should look like this:

session_start();
if (!$_COOKIE['user']) {
   if ($_SESSION['userinfo']) {
      $userinfo = $_SESSION['userinfo'];
      setcookie('user', $userinfo['user'], time()+36000*24*365);
      header('Location: ' . $_SERVER['PHP_SELF']);   
   } else {
     echo "you are not logged in!"; exit;
   }
} else {

echo " congratz you are logged in, " . $_COOKIE['user'];
?>

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users