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

Edited by ginerjm
Link to comment
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.