rascle Posted August 23, 2009 Share Posted August 23, 2009 Hi I am trying to create something where when the users login, if they select a "Remember Me" button then the script will create 2 cookies, one with the password and one with the user, the script is the sent to another page (the index page) and there is a piece of script there to see if the cookie has been set. I know that you must set the cookie first (before any HTML output) so i have put the cookies into a function, here is the code for the login page: <?php function setusercookie($username,$password){ setcookie("loginforeveruser","$username",time()+60*60*24*30); setcookie("loginforeverpass","$password",time()+60*60*24*30); } session_start(); if(isset($_POST["login"])){ $username = $_POST['username']; $password = $_POST['password']; $loginforever = $_POST['loginforever']; $attempt = mysql_query("SELECT * FROM `members` WHERE `username` = '$username' AND `password` = '$password'") or die(mysql_error()); $isusertrue = mysql_num_rows($attempt); if($isusertrue == 0){ echo "Username Or Password is incorrect."; } else{ // TELL USER WE ARE LOGGING THEM IN echo'<br /><br /><br /><div id="userloginloading"><br/><center><font size="5">We are now logging you in<br />Loading</font><br /><img src="http://rasclerhys.com/webimages/loadbar.gif" alt="Loading"><br />Please Wait</center><br/></div>'; $logindetails = mysql_fetch_array($attempt); if($loginforever == on){ setusercookie($logindetails['username'],$logindetails['password']); echo "<meta http-equiv='Refresh' content='2; URL=index.php'/>"; } else{ echo "<meta http-equiv='Refresh' content='2; URL=index.php'/>"; $_SESSION['username'] = $logindetails['username']; $_SESSION['password'] = $logindetails['password']; } } else{ echo' <fieldset><legend><font size="5">Login</legend> </font><font size="4"> <form name="login" action="login.php" method="post"> <center><input type="text" name="username" class="input2" size="50" value="Username" onfocus="setStyle(this.id)" onblur="setStyleback(this.id)" id="user"> <br /> <input type="password" name="password" class="input1" size="50" value="Password" onfocus="setStyle(this.id)" id="pass" onblur="setStylebackp(this.id)"> <br /> <font size="3">Log Me In Forever</font><br /> <input type="checkbox" name="loginforever" class="logforever"><br /><br /> <input type="submit" class="submit" value="Login" name="login" onMouseover="fixedtooltip(\'Log Me In!\', this, event, \'200px\')" onMouseout="delayhidetip()"></center> </form> </font> </fieldset> '; } } Everything works fine and the function parameters are recieved properly, however the cookie doesnt set. Here is the code on the index page to see if the cookie is set: <?php echo $_COOKIE["loginforeveruser"]; echo $_COOKIE["loginforeverpass"];?> I have tried everything and have not found any obvious errors in the code (like spelling mistakes) but was wondering if anyone else could help me. THanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/171540-solved-cookies-help/ Share on other sites More sharing options...
alex3 Posted August 23, 2009 Share Posted August 23, 2009 You've said you need to set cookies before HTML output, look at your script; that's what you're doing! You may have defined the function before the HTML output, but your running the function, and therefore trying to set cookies, after output. This is, as you correctly stated, not allowed. Quote Link to comment https://forums.phpfreaks.com/topic/171540-solved-cookies-help/#findComment-904634 Share on other sites More sharing options...
rascle Posted August 23, 2009 Author Share Posted August 23, 2009 I thought that was the case, but how could i change it so that it does work??? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/171540-solved-cookies-help/#findComment-904636 Share on other sites More sharing options...
corbin Posted August 23, 2009 Share Posted August 23, 2009 else{ // TELL USER WE ARE LOGGING THEM IN echo'<br /><br /><br /><div id="userloginloading"><br/><center><font size="5">We are now logging you in<br />Loading</font><br /><img src="http://rasclerhys.com/webimages/loadbar.gif" alt="Loading"><br />Please Wait</center><br/></div>'; $logindetails = mysql_fetch_array($attempt); if($loginforever == on){ setusercookie($logindetails['username'],$logindetails['password']); echo "<meta http-equiv='Refresh' content='2; URL=index.php'/>"; } To: else{ if($loginforever == on){ setusercookie($logindetails['username'],$logindetails['password']); echo "<meta http-equiv='Refresh' content='2; URL=index.php'/>"; } else{ echo "<meta http-equiv='Refresh' content='2; URL=index.php'/>"; $_SESSION['username'] = $logindetails['username']; $_SESSION['password'] = $logindetails['password']; } // TELL USER WE ARE LOGGING THEM IN echo'<br /><br /><br /><div id="userloginloading"><br/><center><font size="5">We are now logging you in<br />Loading</font><br /><img src="http://rasclerhys.com/webimages/loadbar.gif" alt="Loading"><br />Please Wait</center><br/></div>'; $logindetails = mysql_fetch_array($attempt); Quote Link to comment https://forums.phpfreaks.com/topic/171540-solved-cookies-help/#findComment-904650 Share on other sites More sharing options...
rascle Posted August 23, 2009 Author Share Posted August 23, 2009 Sorry, i dont understand what you want me to change? When i changed what you said the code seemed to be all other the place and didnt display anything. Could you please write the full code you think would work. Thanks Alot Quote Link to comment https://forums.phpfreaks.com/topic/171540-solved-cookies-help/#findComment-904659 Share on other sites More sharing options...
corbin Posted August 23, 2009 Share Posted August 23, 2009 Currently you're doing: Output HTML Set cookie. Why not do: Setcookie Output HTML Quote Link to comment https://forums.phpfreaks.com/topic/171540-solved-cookies-help/#findComment-904663 Share on other sites More sharing options...
rascle Posted August 24, 2009 Author Share Posted August 24, 2009 Thanks, After ages of recoding it i have finally got it. Quote Link to comment https://forums.phpfreaks.com/topic/171540-solved-cookies-help/#findComment-905160 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.