Jump to content

Cookies?


ShoeLace1291

Recommended Posts

How would I set a cookie to remember a user's information if they have a checkbox marked?  This is my current login script:

<?php
require_once('config.php');
// Use session variable on this page. This function must put on the top of page.
session_start();


$message="";
//Login Section.
$Login=$_POST['submit'];
if($Login){ // If clicked on Login button.
$username=$_POST['username'];
$password=md5($_POST['password']); // Encrypt password with md5() function.


// Check matching of username and password.
$result=mysql_query("select * from vbb_members where username='$username' and password='$password'");
if(mysql_num_rows($result)!='0'){ // If match.
session_register("username"); // Craete session username.
mysql_query("UPDATE vbb_members SET logged_in=logged_in+1 WHERE username='$username'");

header("location:index.php"); // Re-direct to main.php
exit;
}else{ // If not match.
$message="--- Incorrect Username or Password ---";
}

} // End Login authorize check.
?> 


<? echo $message; ?> 
<table>
<tr>
<td>User : </td>
<td><input name="username" type="text" id="username" /></td>
</tr>
<tr>
<td>Password : </td>
<td><input name="password" type="password" id="password" /></td>
</tr>
</table>
<input name="Login" type="submit" id="Login" value="Login" />
</form>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/45146-cookies/
Share on other sites

You add in the checkbox in to your form and give it a name of rememberMe and value of true. THe following html should do:
[code]<input type="checkbox" name="rememberMe" value="true" />

 

Then in your PHP code when you process the data submitted from the form you check that $_POST['rememberMe'] exists and equals to 'true', if it exists and equals to 'true' then set the rememberMe cookie.

 

Here is your new code:

<?php

require_once('config.php');

// Use session variable on this page. This function must put on the top of page.
session_start();

$message = '';

//Login Section.
if(isset($_POST['username']) && isset($_POST['password']))
{
    $username = $_POST['username'];
    $password = md5($_POST['password']); // Encrypt password with md5() function.

    // RemeberMe Feature
    if(isset($_POST['rememberMe']) && $_POST['rememberMe'] == 'true')
    {
        // prepare cookie data
        $c['username'] = $username;
        $c['password'] = $password;

        // // set the cookie
        // name: remeberMe
        // expire after 1 year
        setcookie('rememberMe', serialize($c), time()+3600*24*365);
    }

    // Check matching of username and password.
    $sql = "SELECT 8 FROM vbb_members WHERE username='$username' AND password='$password'";
    $result = mysql_query($sql);

    // If match.
    if(mysql_num_rows($result)!= 0)
    {
        $_SESSION['username'] $username; // create session username

        mysql_query("UPDATE vbb_members SET logged_in=logged_in+1 WHERE username='$username'");

        header("Location: index.php"); // Re-direct to main.php
        exit;
    }
    // If not match.
    else
    {
        $message = "--- Incorrect Username or Password ---";
    }

} // End Login authorize check.
?>


<? echo $message; ?>
<table>
<tr>
<td>User : </td>
<td><input name="username" type="text" id="username" /></td>
</tr>
<tr>
<td>Password : </td>
<td><input name="password" type="password" id="password" /></td>
</tr>
</table>
Remeber Me? <input type="checkbox" name="rememberMe" value="true" />
<input name="Login" type="submit" id="Login" value="Login" />
</form>
</body>
</html>

The code will set a cookie called 'rememberMe' when you want to use the cookie you want to use the $_COOKIE['remeberMe'] variable. However before you can use the values set in the cookie you must unserialize the data, as I serialized the array when setting the cookie

 

So in order to use the cookie data you'll want to do this:

// unserialize the serialized data in the cookie
// rm short for rememberMe
$rm = unserialize($_COOKIE['rememberMe']);

// when you unserialize the cookie the $rm variable will hold an array 'username' and 'password':
//Username variable: $rm['username'] <--- contains the username string
//Password variable: $rm['password '] <--- contains the md5 hash of the password

[/code]

Link to comment
https://forums.phpfreaks.com/topic/45146-cookies/#findComment-219227
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.