Jump to content

setting a simple cookie problem


mrmcg

Recommended Posts

Hi

 

Im really new to php and have been having a big headache from this latest problem im facing.

 

All I want to do is save the username to a cookie on one page then access that cookie in the next few pages to insert the username into mysql through a form.

 

Have a look at the code i have so far..................It seems so simple.............. It just won't save the cookie to my computer

 

logger.php

<?php
/* These are our valid username and passwords */
$user = 'jonny4';
$pass = 'delafoo';

if (isset($_POST['username'])) {
    
    if ($_POST['username'] == $user) {    
        
        if (isset($_POST['rememberme'])) {
            /* Set cookie to last 1 year */
            setcookie('username', $_POST['username'], time()+60*60, '/', 'www.kelly.com');
           
        } else {
            /* Cookie expires when browser closes */
            setcookie('username', $_POST['username'], false, '/', 'www.kelly.com');
            }
        header('Location: form/form.html');
        
    } else {
        echo 'Username/Password Invalid';
    }
    
} else {
    echo 'You must supply a username and password.';
}
?>

Index.html

<p>
 <h2>User Login </h2>
  <form name="login" method="post" action="logger.php">
   Username: <input type="text" name="username">          
   
   <input type="checkbox" name="rememberme" value="1" checked="yes" hidden><br>
   <input type="submit" name="submit" value="Login!">
  </form>
 </p>

Any help with this would be greatly appreciated as its hurting my head now.

Lets see if a lie down will help

 

Thanks in advance

 

Link to comment
https://forums.phpfreaks.com/topic/287519-setting-a-simple-cookie-problem/
Share on other sites

1 - you are only saving the cookie for one hour.

2 - Does your browser have cookies disabled?

3 - How do you know it is not being saved?

 

PS - When you say it is not setting the cookie, is that a fact or are you simply saying that you are getting your error message instead?

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.