Cookie

Wont show up

Page 1 of 1

9 Replies - 1167 Views - Last Post: 21 September 2009 - 04:53 AM Rate Topic: -----

#1 ಠ_ಠ   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 26-September 08

Cookie

Post icon  Posted 17 September 2009 - 02:25 PM

I have a basic script to test a cookie.

Login.php

<?php
include('connect.php');


$username = $_POST['username'];
$password = $_POST['password'];

mysql_select_db("system");


$result = mysql_query("SELECT username,password FROM users where username = '".$username."'") or die(mysql_error());

$row = mysql_fetch_array($result);

$DB_Password = $row['password'];
$DB_Username = $row['username'];

echo "<br>$password :". $DB_Password."<br>";
echo "$username :". $DB_Username;

if ($DB_Username == $username && $password == $DB_Password)
{
setcookie("user", $username, time()+3600);
}

?>





Index.php
echo "Welcome " . $_COOKIE["user"] . "!<br />";




Error:

Undefined index: user in C:\wamp\www\scripts\menu.php on line 14

I looked in my browser and the cookie is there with the right name but it wont show up on the page.

Is This A Good Question/Topic? 0
  • +

Replies To: Cookie

#2 Wimpy   User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Cookie

Posted 17 September 2009 - 02:35 PM

Are you completely sure the cookie is set? Try to change this:
if ($DB_Username == $username && $password == $DB_Password)
{
setcookie("user", $username, time()+3600);
}
to:
if ($DB_Username == $username && $password == $DB_Password)
{
	if(setcookie("user", $username, time()+3600))
	{
		echo "Cookie was set";
	}
}
and see if you get the message "Cookie was set". :)
Was This Post Helpful? 0
  • +
  • -

#3 RPGonzo   User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Cookie

Posted 17 September 2009 - 02:50 PM

Like wimpy said verify the setting of the cookie ... you can also just use print_r($_COOKIE); to see ALL the information set in the cookie to double check as well ... but why not just use a session variable instead of having to set cookies?

This post has been edited by RPGonzo: 17 September 2009 - 02:52 PM

Was This Post Helpful? 0
  • +
  • -

#4 ಠ_ಠ   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 26-September 08

Re: Cookie

Posted 17 September 2009 - 02:57 PM

I cleared my cookies and replaced my code, when i log in i get "MartinCookie was set" but no difference in the error.
Heres the full login code:


<?php
include('connect.php');


$username = $_POST['username'];
$password = $_POST['password'];

mysql_select_db("system");


$result = mysql_query("SELECT username,password FROM users where username = '".$username."'") or die(mysql_error());

$row = mysql_fetch_array($result);

$DB_Password = $row['password'];
$DB_Username = $row['username'];

echo "<br>$password :". $DB_Password."<br>";
echo "$username :". $DB_Username;

if ($DB_Username == $username && $password == $DB_Password)
{
	if(setcookie("user",$username, time()+3600))
	{
		echo "Cookie was set";
	}
}

?>




This is the output RPGonzo:
"Array ( [user] => Martin ) 1"



"but why not just use a session variable instead of having to set cookies?"

I'm very new to storing user data temporary but right now i just want to get it working then inprove it :).

This post has been edited by ಠ_ಠ: 17 September 2009 - 03:00 PM

Was This Post Helpful? 0
  • +
  • -

#5 RPGonzo   User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Cookie

Posted 17 September 2009 - 03:02 PM

$username = isset($_COOKIE['user']) ? $_COOKIE['user'] : "undefined";
echo "Welcome " . $username . "!<br />";



might always be a good idea to do a isset on the var you are trying to pull ... you will avoid notices in the long run ... unfortunately ... if it doesnt echo im not to sure because according to the output its there :-/
Was This Post Helpful? 0
  • +
  • -

#6 ಠ_ಠ   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 26-September 08

Re: Cookie

Posted 17 September 2009 - 03:20 PM

When i set the cookie on the index page it works, so the function is pulling the var out but when i use the exact same code in the login page it goes all wrong.

setcookie("user","AName",time()+3600)


Was This Post Helpful? 0
  • +
  • -

#7 RudiVisser   User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,566
  • Joined: 05-June 09

Re: Cookie

Posted 18 September 2009 - 02:32 AM

wait, are you setting the cookie on the login page??

Sorry, I haven't read over the code.. But if you are, cookies only work on the next pageload after they've been set.
Was This Post Helpful? 0
  • +
  • -

#8 ಠ_ಠ   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 26-September 08

Re: Cookie

Posted 18 September 2009 - 06:10 AM

The information i submit on the index.php page brings you to the login.php script to login.
Then the login script redirects you to the index page
Was This Post Helpful? 0
  • +
  • -

#9 ShaneK   User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Cookie

Posted 18 September 2009 - 08:59 AM

Try this one:
setcookie("user","AName",time()+3600, "/")

Basically, I believe, makes the cookie accessible for the entire site.
(I had to do this for cookies on TibiaLottery)

Yours,
Shane~

This post has been edited by ShaneK: 18 September 2009 - 09:02 AM

Was This Post Helpful? 1
  • +
  • -

#10 ಠ_ಠ   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 26-September 08

Re: Cookie

Posted 21 September 2009 - 04:53 AM

That looks like it might work, i will try it when i get home.
But it works in a little test site i made up.

Thanks Shane

This post has been edited by ಠ_ಠ: 21 September 2009 - 04:53 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1