mrmcg Posted April 4, 2014 Share Posted April 4, 2014 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 More sharing options...
ginerjm Posted April 4, 2014 Share Posted April 4, 2014 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? Link to comment https://forums.phpfreaks.com/topic/287519-setting-a-simple-cookie-problem/#findComment-1474952 Share on other sites More sharing options...
Ch0cu3r Posted April 4, 2014 Share Posted April 4, 2014 No sure try setting the cookie without specifying the domain. setcookie('username', $_POST['username'], time()+60*60, '/'); Link to comment https://forums.phpfreaks.com/topic/287519-setting-a-simple-cookie-problem/#findComment-1474953 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.