Jump to content

Cookies


neex1233
 Share

Recommended Posts

This has been driving me crazy for hours!

I am trying to set a cookie: 

setcookie("username", $_POST['user']); 

And for a while it wasn't working, then it started randomly working. Now, when I try to use

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

PHP is saying the cookie does not exist. PLEASE help! Thanks.

Link to comment
Share on other sites

Dont understand u at all. This "It's not before any tags, and those tags aren't helping" - whose tags ?

"on the page I'm having the problem, and it doesn't do anything. However, if I do it on a different page, it works." - what do you mean with diferent ?

Link to comment
Share on other sites

<?php 
/* $con make a connection with database */
$con=mysql_connect("localhost","root",""); 

//select  database
mysql_select_db("users"); 

/* Below two commands will store the data in variables came from form input */
$username=$_POST['user'];
$password=$_POST['pass'];

/* below two commands are sql injection which stops extra characters as input */
$user=mysql_real_escape_string($username);
$pass=mysql_real_escape_string($password);

$query=mysql_query("SELECT * FROM users where
user='$user' AND 
pass='$pass' "); 

$count=mysql_num_rows($query);
if($count==1) 
/* $count checks if username and password are in same row */
{ 
echo "Login Successful";
$hour = 86400*365;  
/* $hour sets cookie storage time for 1 hour */

/* setcookie() function sets cookie after login */
ob_start();
ob_clean();
setcookie("username", $_POST['user']); 
setcookie("password", $_POST['pass']);

header("location:/"); 
/* header() funtion redirect user to members page */
}
else
{ 
echo "Username or password is incorrect";
}
?>

 

 

And

 

<?php
ob_start();
ob_clean();
$user = $_COOKIE['username'];
if(isset($user))
{

}
else 
{
header("location: /users/login.php");
}
?>

 

I know the cookies exist, because I looked in Add 'n Edit Cookies.

Link to comment
Share on other sites

It's not the actual making of the cookie that's the problem, it's the detection of the cookie. The cookie is made fine, I've checked in Add 'n Edit Cookies, but this code:

<?php
ob_start();
ob_clean();
$user = $_COOKIE['username'];
if(isset($user))
{

}
else 
{
header("location: /users/login.php");
}
?>

isn't working. The code works on other pages, though.

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.