Jump to content

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/10106-php-cookies/
Share on other sites

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...
Link to comment
https://forums.phpfreaks.com/topic/10106-php-cookies/#findComment-37602
Share on other sites

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:

[code]
$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;


}
[/code]

and since you have session_start(); in your login_success.php as well, you can access the user information with $_SESSION['userinfo']
Link to comment
https://forums.phpfreaks.com/topic/10106-php-cookies/#findComment-37612
Share on other sites

Now my scripts look like this:

Login.php

[code]
<?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>
[/code]

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

Login_sucess.php

[code]
<?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>
[/code]

R These both right?

- Thanks, Daniel
Link to comment
https://forums.phpfreaks.com/topic/10106-php-cookies/#findComment-37629
Share on other sites

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

[b]session_start();[/b]

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

then you were supposed replace

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

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

inside your if statement.
Link to comment
https://forums.phpfreaks.com/topic/10106-php-cookies/#findComment-37632
Share on other sites

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:
[code]
<?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>
[/code]

Login_success.php:
[code]
<?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>
[/code]

- Cheers, Daniel

Please Help
Link to comment
https://forums.phpfreaks.com/topic/10106-php-cookies/#findComment-37653
Share on other sites

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>
Link to comment
https://forums.phpfreaks.com/topic/10106-php-cookies/#findComment-37697
Share on other sites

[!--quoteo(post=375975:date=May 22 2006, 01:21 AM:name=Drezard)--][div class=\'quotetop\']QUOTE(Drezard @ May 22 2006, 01:21 AM) [snapback]375975[/snapback][/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[b][!--coloro:red--][span style=\"color:red\"][!--/coloro--]c[!--colorc--][/span][!--/colorc--][/b]ess.php with one c but in your code up above the header calls login_su[b][!--coloro:red--][span style=\"color:red\"][!--/coloro--]cc[!--colorc--][/span][!--/colorc--][/b]ess.php with two c's

also here:

[code]
session_start();
setcookie('user', $user, time()+36000*24*365);
if (isset($_GET[$user]))
{
    $user = $_GET[$user];
    }
setcookie('user', $user, time()+36000*24*365);
[/code]
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,

[code]
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);
[/code]
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:

[code]
if (isset($_GET[$user]))
{
    $user = $_GET[$user];
    }
setcookie('user', $user, time()+36000*24*365);
[/code]
Link to comment
https://forums.phpfreaks.com/topic/10106-php-cookies/#findComment-38098
Share on other sites

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

[code]
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'];
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/10106-php-cookies/#findComment-38101
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.