FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


setcookie with PHP





minty
Code:

// if login is ok then we add a cookie
  $pwd = stripslashes($pwd);
  $hour = time() + 600;
  setcookie("minty_frihost_net", $pwd, $hour);

if ($_COOKIE['minty_frihost_net']){
  sleep(5);
  header("Location: /index2.php");
}else{
echo "No cookie set";
}


Is this setup properly Question I have the
Code:
if ($_COOKIE['minty_frihost_net'])

to determine whether the cookie is really being set. I know I have my machine and browser setup to allow it, for I have other sites that leave cookies...Any and all ideas are greatly appreciated.

BTW, the cookie is set after authentication passes and is used to keep the user from having to log back in when they goto different pages within my site. I believe this is called a "session"? Question ?

Thanks again,

-minty
Star Wars Fanatic
To check if the cookie is set:

Code:
if(isset($_COOKIE['minty_frihost_net']))  {


Other than that, I think the rest of the code is fine.
minty
Hmmm....I did not use the isset() function because I thought that it was only useful at the beginning of a PHP file...

Nonetheless, I tried using the isset(); function to determine if it is set, and I still was not redirected. Instead the page displayed "No cookie set" as it should if no cookie as been made...

If this does not work, how else can I keep users logged in? Or track users?

Thanks,

--minty
imagefree
minty wrote:
Hmmm....I did not use the isset() function because I thought that it was only useful at the beginning of a PHP file...

Nonetheless, I tried using the isset(); function to determine if it is set, and I still was not redirected. Instead the page displayed "No cookie set" as it should if no cookie as been made...

If this does not work, how else can I keep users logged in? Or track users?

Thanks,

--minty



Well you are doing a big big mistake.
If you are setting a cookie from a file llike page.php , it will be accessible afterwords in when you try to access the cookie from any other script (provided that the page page.php is already visited.)

Hope you get what i want to say. ( What i know is you cant access your cookies from the same script in which you once set it.)

Every other thing is good , i think. And script should work with or without ISSET , but move it to the any other page. Very Happy
minty
Well here is the page that you are redirected to upon authentication

Code:

<?php

if(isset($_COOKIE['minty_frihost_net']))
{
echo "<center>You have been authorized to view this page</center>";
}else{
header("Location: ./index.php");
}
?>


Each time I am redirected back to the authenticating page. Any ideas?

--minty
Peterssidan
Quote:
Code:
$hour = time() + 600

Are you sure that the cookie is being set? If the server time differs from yours this can cause problem. your code se the cookie stay alive ten minutes but if you lives one timezone away it can be 50 minutes before and the cookie are not set.

I can't see anything other than that that can cause the problem.
Star Wars Fanatic
Thanks for that info, imagefree, I forgot to mention it Confused

Minty, make sure to check your cookies after you run the script to see if a cookie is actually set.
What I mean is go to the browser you are using to test it, and look in your cookies for that particular cookie.
If you can't find it, it means there is something wrong with your cookie setting. If you can, that means there is something wrong with checking it.
minty
How can I determine what time it is on the server?? So that I can determine what the difference is on my computer.

Speaking about time differences, how would I make cookies functional for people from all different time zones??

--minty
minty
Alirhgt, so I changed the function to
Code:

$hour = time() + 3600;


This works!!!

Thanks a lot guys. You have been very helpful.

One last question on top of my previous question, what are sessions and how are they setup?

--minty
Stubru Freak
minty wrote:
Alirhgt, so I changed the function to
Code:

$hour = time() + 3600;


This works!!!

Thanks a lot guys. You have been very helpful.

One last question on top of my previous question, what are sessions and how are they setup?

--minty


Sessions are a way to just store session-related data without worrying about cookies. PHP will set a cookie with an ID for you, and store any data you set under that ID. So next time a page is requested using a cookie with that ID, the session data is reloaded. For more information, see http://www.php.net/session.
Related topics
HELP: setcookie() and header problems
HTTP AUTH with PHP and mySQL
using cookies in PHP
[PHP] phpbb registration - add to other db
[PHP] Dynamic content
Php parsing error! Need help
one line securing of php pages (login of users) (user auth)
PHP script problem
PHP and cookies. Simple problem.
PHP login script problem
PHP and MySQL FAQ - Read Before posting!
Javascript, cookies, Opera; PROBLEM
PHP Cookies Not Working
Php users counter script
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.