Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/53246-php-cookie-help/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/53246-php-cookie-help/#findComment-263185
Share on other sites

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

?>

 

Link to comment
https://forums.phpfreaks.com/topic/53246-php-cookie-help/#findComment-263765
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/53246-php-cookie-help/#findComment-263782
Share on other sites

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

?>

Link to comment
https://forums.phpfreaks.com/topic/53246-php-cookie-help/#findComment-263794
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.