NSW42 Posted May 28, 2007 Share Posted May 28, 2007 Hey, not sure if this is the right section to post my issue, but will go ahead anyway. with my website sometimes users login in and yet when they try to enter it takes them back to the login page telling them there not logged in, now im assuming this has something to do with the cookie keeping, but then again im still a newy and still learning it all, but i will paste my cookie code to see if it may hold the key to my problem as not all users get this issue. many thanks... setcookie("username","$_POST[username]",time()+30000000); setcookie("password","$md5_password",time()+30000000); echo "<meta http-equiv=refresh content=\"5;url=/\">"; print("your now being logged in!"); } } else { print("Your are already loggedin!"); Quote Link to comment https://forums.phpfreaks.com/topic/53246-php-cookie-help/ Share on other sites More sharing options...
Vinze Posted May 28, 2007 Share Posted May 28, 2007 Perhaps you could use sessions? // This should be at the very beginning of your code, before anything is output session_start(); // Blabla lots of code $_SESSION['username'] = $_POST[username]; $_SESSION['password'] = $md5_password; // Now every page that calls session_start() at the beginning has $_SESSION set to these values echo "<meta http-equiv=refresh content=\"5;url=/\">"; print("your now being logged in!"); Quote Link to comment https://forums.phpfreaks.com/topic/53246-php-cookie-help/#findComment-263185 Share on other sites More sharing options...
NSW42 Posted May 29, 2007 Author Share Posted May 29, 2007 well this is pretty much my script below if u can enlighten me some more on it..thanks <?php ob_start(); include("functions.php"); if(@$action=='login') { if($_COOKIE['username']=="" AND $_COOKIE['password']=="") { $md5_password = md5($_POST['password']); $sql = mysql_query("SELECT * FROM users WHERE username = '$_POST[username]' AND password = '$md5_password'") OR DIE("Sorry there is a mysql error."); $row = mysql_fetch_array($sql); $numrows = mysql_num_rows($sql); if($_POST[password]=="") { print("No password Entered."); login_form(); exit; } elseif($_POST[username]=="") { print("No username Entered."); login_form(); exit; } elseif($numrows == "0") { print("Username and Password doesnt match!"); login_form(); exit; } else { setcookie("username","$_POST[username]",time()+30000000); setcookie("password","$md5_password",time()+30000000); echo "<meta http-equiv=refresh content=\"5;url=/\">"; print("your now being logged in!"); } } else { print("Your are already loggedin!"); } } if(@$action=="") { login_form(); } ob_end_flush(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/53246-php-cookie-help/#findComment-263765 Share on other sites More sharing options...
paul2463 Posted May 29, 2007 Share Posted May 29, 2007 if you use the session method, this gets around any user not having cookies set on their client machine by using PHP's session variables, use it like you did cookies but with a couple of slight changes <?php session_start(); //first command on any page that needs the logged in data username and password ob_start(); include("functions.php"); if(@$action=='login') { if($_SESSION['username']=="" AND $_SESSION['password']=="") //checks to see if the session variables are set { $md5_password = md5($_POST['password']); $sql = mysql_query("SELECT * FROM users WHERE username = '$_POST[username]' AND password = '$md5_password'") OR DIE("Sorry there is a mysql error."); $row = mysql_fetch_array($sql); $numrows = mysql_num_rows($sql); if($_POST[password]=="") { print("No password Entered."); login_form(); exit; } elseif($_POST[username]=="") { print("No username Entered."); login_form(); exit; } elseif($numrows == "0") { print("Username and Password doesnt match!"); login_form(); exit; } else { $_SESSION["username"] = $_POST['username']; //sets the session variable for unsername $_SESSION["password"] = $md5_password; //sets the session variable for md5 password echo "<meta http-equiv=refresh content=\"5;url=/\">"; print("your now being logged in!"); } } else { print("Your are already loggedin!"); } } if(@$action=="") { login_form(); } ob_end_flush(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/53246-php-cookie-help/#findComment-263782 Share on other sites More sharing options...
NSW42 Posted May 29, 2007 Author Share Posted May 29, 2007 ok i tried that, it logged in ok, but the got the result of u need to sigin again ha that got to do with my logout below...thanks again <? setcookie("username",""); setcookie("password",""); header("LOCATION: loggedout.php"); include("config.php"); include("functions.php"); include("$sitepath/html/footer.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/53246-php-cookie-help/#findComment-263794 Share on other sites More sharing options...
NSW42 Posted May 29, 2007 Author Share Posted May 29, 2007 anymore further help on this would be appreciated...thanks Quote Link to comment https://forums.phpfreaks.com/topic/53246-php-cookie-help/#findComment-264263 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.