How to determine if cookie is set

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

How to determine if cookie is set

Post by ddragas »

How to determine if cookie is set
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Code: Select all

print($HTTP_COOKIE_VARSї'Cookiename']);
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Maybe it's silly question but
how to give a neme to a cookie
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

setcookie('name','value',expiry_time,path,domain,is_secure)
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

[php_man]setcookie[/php_man]
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

you name it when you set a cookie

Code: Select all

<?php
setcookie("namehere", $value, time()+expirey in secs);
?>
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

go to php.net and type in setcookie and read on it.

clearly you need to know the knowledge before u tamper with cookies cause it poses alot of security risks.

edit

man beaten by three people, usually its just feyd

haha
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

here is my whole problem.

It's about validation, and user pages.

Here is code for seting cookie:

Code: Select all

<?php
session_start();
$broj = $_SESSION['broj'] ;


$e_mail = $_POST['email'];
$lozink_a = $_POST['lozinka'];

$mail=$e_mail;
$pass=md5 ($lozink_a);

if(isset($mail) and isset($pass)){

$expires_soon = date("l, d-M-y H:i:s", time() + 31536000); 
setcookie( "email=$mail;  expires=$expires_soon;"); 
setcookie( "name=valid,  lozinka=$pass;  expires=$expires_soon;"); 

}

?>
and code for retrive value from cookie:

Code: Select all

if(isset($_COOKIE["valid"])){

$email = $_COOKIE['email']; 
$lozinka = $_COOKIE['lozinka'];
} else{
$email =$e_mail;
$lozinka = md5 ($lozink_a);}
Now pages are organised in way that every time user comes on this page with code from above (after finished editing his profile) his data should be retrived, and autification that this user is right one.

In this way when user comes back on this page it ask me to login again, becouse value from cookie was not retrived.

Don't know if I've explain my self enough. If you have any questions feel free to ask

Thank you
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

First off, use

Code: Select all

tags. Secondly, here is your problem. You are setting 2 cookies. Why? Secondly, setcookie works like this:

Code: Select all

<?php 
setcookie("name", "value", time()+3600); //notice how I use quotes 
?>
And you don't have to say name= and value=.

Good luck!
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

I'm setting 2 cookies becouse I have two values: e-mail and password. Can I put two values in same cookie?
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Why would you want to put 2 values? If you have 1 value, you can get the rest of them from your database or file or wherever your values are stored?

Registration based entirely on cookies will NOT work (this seems like what you're doing). Some browsers don't even accept cookies, have you thought about that? I suggest using a database to store user information.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

evilmonkey wrote:

Code: Select all

<?php 
setcookie("name", "value", time()+3600);?>
Instead of time()+3600 i prefer strtotime("+1 day");
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Thank you all
Post Reply