Jump to content

Recommended Posts

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 :)

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

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.

Link to comment
https://forums.phpfreaks.com/topic/171540-solved-cookies-help/#findComment-904634
Share on other sites

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);

 

Link to comment
https://forums.phpfreaks.com/topic/171540-solved-cookies-help/#findComment-904650
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.