Jump to content

Cookie Problems =\


guitarist809

Recommended Posts

Hello,

 

I'm trying to set two cookies on my site, containing a username and password so I can retrieve them for later use

 

This function doesn't set any cookies i give it

 

function session_init($username, $password)
{
	setcookie("tripdb_username", $rs['username'], time()+3600);
	setcookie("tripdb_password", $rs['password'], time()+3600);

	echo "cookies set!";
	echo "<pre>";
print_r($_COOKIE);

 

I'm using the print_r to show all cookies, all it shows is a PHPSESSID :(

 

Why isn't this working?

Link to comment
Share on other sites

You can't use a cookie until the user refreshes the page.

 

From the manual:

"Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires...."

 

Yea I saw that. I refreshed the page w/o the setcookie function and still didn't see the cookies =\. Just the PHPSESSID...

Link to comment
Share on other sites

yeah well what is your page doing when you REFRESH it?

 

exactly what it did the first time :) so you will get the same results won't you?

 

what you need to do is make an if/else statement to set the cookie if it isn't already set, and display the cookie if it is set, etc... you get it

 

 

whats going with ur page now is that it keeps setting and overwriting ur cookie

 

*EDIT sorry i didn't read all of your post about how u removed the setcookie function

 

try this when echoing the cookie:

$var = $_COOKIE['urcookiename'];

echo $var;

 

because when you print_r $cookie, i think only works if u SET the cookie on that page

use this other syntax and it will call on the cookie that is already set

Link to comment
Share on other sites

Basically it goes as follows:

login.php => input username/password into a form. Submit form

login.php => Make sure username/password is valid w/ mysql database. if it is, set two cookies (username and password) and do header('location: main.php');

main.php => check cookies ($_COOKIE['username'], $_COOKIE['password']) - make sure those are set. (They wern't so I did a print_r to see what was in $_COOKIE, but nothing was there...

main.php => if no cookies are set (or cookies contain invalid information)... header('location: login.php');

 

 

Hope that helps with my prob...

Link to comment
Share on other sites

You can't use a cookie until the user refreshes the page.

JavaScript Cookies ??

 

But I would Suggest You Dont Use Cookies Use Sessions Its Much more Simple and also more secure.

 

I did use sessions, but they would self-terminate when I closed out of my browser. With cookies you can set a delete time.

Link to comment
Share on other sites

i had a similar problem when i was playing around with cookies a while ago

 

only solution i found was to only set the cookie on ONE page

and then call the cookie on ANOTHER page

 

not to do this on the same page

 

also sometimes if u refresh it TWICE then it works...

play around with those things

Link to comment
Share on other sites

You can't use a cookie until the user refreshes the page.

JavaScript Cookies ??

 

But I would Suggest You Dont Use Cookies Use Sessions Its Much more Simple and also more secure.

 

I did use sessions, but they would self-terminate when I closed out of my browser. With cookies you can set a delete time.

/* set the cache expire to 30 minutes */
session_cache_expire(30);
$cache_expire = session_cache_expire();

you can change the max lifetime for a session with the function ini_set().

 

<?php 
ini_set("session.gc_maxlifetime", "18000"); 
?>

Link to comment
Share on other sites

You can't use a cookie until the user refreshes the page.

JavaScript Cookies ??

 

But I would Suggest You Dont Use Cookies Use Sessions Its Much more Simple and also more secure.

 

I did use sessions, but they would self-terminate when I closed out of my browser. With cookies you can set a delete time.

/* set the cache expire to 30 minutes */
session_cache_expire(30);
$cache_expire = session_cache_expire();

you can change the max lifetime for a session with the function ini_set().

 

<?php 
ini_set("session.gc_maxlifetime", "18000"); 
?>

 

That code did'nt do anything :(

 

As soon as I exit my browser and start it up again i still have to re-authenticate

Link to comment
Share on other sites

But I would Suggest You Dont Use Cookies Use Sessions Its Much more Simple and also more secure.

 

Security with cookies is controversial, do some research guitarist on cookie "security" as long as nothing private or no sensitive information is stored in cookies, its a perfectly great way to store information for long periods of time

 

only problem is that sometimes users choose to not accept cookies, but that is rare, for those extra paranoid internet users out there, cookies and the internet go together like bread and butter

 

*a note on the session max time, session will ALWAYS expire when the browser window is closed

all the code the other dude gave u is define when the session will expire while the browser window is still open

Link to comment
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.