Jump to content

Recommended Posts

Hey, I'm trying to get cookies to work on other pages of my website so when a user logs in, the user gets other functions on other webpages... So I have two codes to do this. One that lets the user log in, and if the information is right, a cookies are set.

 

<?php


mysql_connect ("localblah", "localblah", "localblah) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("localblah");



$userentry=$_POST['username'];
$passentry=$_POST['password'];
$hashedpwentry=md5($passentry);


$sql = "SELECT * FROM users WHERE username='$userentry'";

$result = mysql_query($sql); 

$userinfo = mysql_fetch_assoc($result) or die("Your username does not exist!");


if($hashedpwentry != $userinfo['password'])
{

die("Your login was unsuccessful, please make sure your password is correct.");
}
else
{
setcookie("username", $userinfo['username']);
setcookie("password", $userinfo['password']);
setcookie("userid", $userinfo['userid']);
setcookie("equip", $userinfo['equip']);
setcookie("name", $userinfo['name']);



}
?>

<html>
<head>
<title>Login Processed!</title>
</head>
<body>


<a href='home.php' target='mainFrame'>Please click here to be redirected.</a>


</body>
</html>

 

I'm pretty sure the error is some way I'm setting up, but I don't know what. Is there something I'm forgetting, and I need a function that will move the cookies to other variables, but I don't know what.

 

And if there is a better way to do what I'm doing, just give me some feedback.

 

Help is appreciated. And added to make sure if needed, here is the if code for getting the cookies I use.

 

if (isset($_COOKIE['userid']))

 

Thanks in advance!

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

You need to set an expiration for the cookie. Otherwise the cookie expires when the session ends. Since you are not initiating a session, then it may be expiring as soon as the page changes - I've never NOT used an expiration, so I'm not 100% sure. try this:

 

<?php

  $daysToLive = 30; //Days until the cookie expires

  setcookie("TestCookie", $value, time()+86400*$daysToLive);
  setcookie("username", $userinfo['username'], time()+86400*$daysToLive);
  setcookie("password", $userinfo['password'], time()+86400*$daysToLive);
  setcookie("userid", $userinfo['userid'], time()+86400*$daysToLive);
  setcookie("equip", $userinfo['equip'], time()+86400*$daysToLive);
  setcookie("name", $userinfo['name'], time()+86400*$daysToLive);

Link to comment
https://forums.phpfreaks.com/topic/45340-solved-cookie-help/#findComment-220146
Share on other sites

Straight besides the fact I editted the mysql_connect for personal reasons, and I forgot a " at the end, but don't worry, it isn't a problem in the script I made.

 

You guys don't mind me asking either, but when I want to log out, how do I make it work with the IF statement correctly.

 

This statement:

if (isset($_COOKIE['userid']))

 

If I use unset to unset the cookie, I'm pretty sure that would not unset the WHOLE global $_cookie array. Is there a certain function to delete the cookie so that people can log out? Or is there a better condition.

Link to comment
https://forums.phpfreaks.com/topic/45340-solved-cookie-help/#findComment-220160
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.