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


PHP and cookies. Simple problem.





ocalhoun
I'm having a hard time getting these cookies to load correctly.

Problem:
First of all, after the user's info checks out, it is time to put the info into cookies, then go to the login_good.php page.
Because there has already been output to the browser at this time, the cookies cannot be set within this page.
Therefore, I use a javascript redirect to a small php page which will send the cookies:
The $_POST data referred to here is correct at this point.
Code:

$_POST['username'] = stripslashes($_POST['username']);
//echo "redirect and cookies";
echo "<script language='javascript'>";
echo "window.location='login_good_pre.php?username=" . $_POST['username'] . "&password=" . $_POST['pass'] . "'";
echo "</script>";

The redirect sends the user (and the $_GET data) to the login_good_pre.php page:
Code:

<?php
$hour = time() * 4;
//This file prepares the cookies for login_good.php, then redirects to it.
setcookie('EqDr_story_ID', $_GET['username'], $hour);
setcookie('EqDr_story_key', $_GET['pass'], $hour);
//header("Location: login_good.php"); //temporarily disabled so I can troubleshoot this page.
echo $_GET['username']; //The echoed result here is correct
echo "<br>";
echo $_GET['password']; //The echoed result here is correct
echo "<hr>";
echo $_COOKIE['EqDr_story_ID']; //No result from echo
echo "<br>";
echo $_COOKIE['EqDr_story_key']; //No result from echo
//echo time() . "<br>"; //correct
//echo $hour; //correct
?>

Obviously, the cookies are not being set properly, or are not being retrieved properly*. I have checked this with 3 computers using 2 different OS's (linux, win2k, winXP) and 7 different browsers (konqueror, firefox1, firefox2, IE6, IE7, opera, galleon), all of which I have made sure are set up to accept all cookies. None of these configurations work.
Why won't the cookies work?
Surely it is some simple, stupid mistake I've made, because I'm as inexperienced with cookies as possible.

*I looked in the browser's cookie cache, and the cookies are not there (although others are). This proves that the cookies are not being received by the browser properly.

Code for me to search for this thread later:
lsfjselkmfsihyslfnm
bladesage
ocalhoun wrote:
I'm having a hard time getting these cookies to load correctly.

Problem:
First of all, after the user's info checks out, it is time to put the info into cookies, then go to the login_good.php page.
Because there has already been output to the browser at this time, the cookies cannot be set within this page.
Therefore, I use a javascript redirect to a small php page which will send the cookies:
The $_POST data referred to here is correct at this point.
Code:

$_POST['username'] = stripslashes($_POST['username']);
//echo "redirect and cookies";
echo "<script language='javascript'>";
echo "window.location='login_good_pre.php?username=" . $_POST['username'] . "&password=" . $_POST['pass'] . "'";
echo "</script>";

The redirect sends the user (and the $_GET data) to the login_good_pre.php page:
Code:

<?php
$hour = time() * 4;
//This file prepares the cookies for login_good.php, then redirects to it.
setcookie('EqDr_story_ID', $_GET['username'], $hour);
setcookie('EqDr_story_key', $_GET['pass'], $hour);
//header("Location: login_good.php"); //temporarily disabled so I can troubleshoot this page.
echo $_GET['username']; //The echoed result here is correct
echo "<br>";
echo $_GET['password']; //The echoed result here is correct
echo "<hr>";
echo $_COOKIE['EqDr_story_ID']; //No result from echo
echo "<br>";
echo $_COOKIE['EqDr_story_key']; //No result from echo
//echo time() . "<br>"; //correct
//echo $hour; //correct
?>

Obviously, the cookies are not being set properly, or are not being retrieved properly*. I have checked this with 3 computers using 2 different OS's (linux, win2k, winXP) and 7 different browsers (konqueror, firefox1, firefox2, IE6, IE7, opera, galleon), all of which I have made sure are set up to accept all cookies. None of these configurations work.
Why won't the cookies work?
Surely it is some simple, stupid mistake I've made, because I'm as inexperienced with cookies as possible.

*I looked in the browser's cookie cache, and the cookies are not there (although others are). This proves that the cookies are not being received by the browser properly.

Code for me to search for this thread later:
lsfjselkmfsihyslfnm


Well, I am not quite sure of the exact problem you are having here, but I will gladly help out with a few pointers.

1) It is quite possible to send header data after output is sent to the browser. By using output buffering, you can start output, change it, add headers, and then erase or send it to the browser. I find this EXTREMELY useful for making my login systems.

To use output buffers, there are only two necessary steps, as well as a few little conveniences.

At the beginning of the document (or at least at some point before *ANY* output is sent), add this one function, with no arguments:
Code:
ob_start();


After that, feel free to use any header(), echo(), or print() data you wish (for example, to change location). At the very end of the document, add this single function:
Code:
ob_end_flush();


Until this function is used, nothing will be sent to the browser! In other words, do whatever you want; add headers in the middle of the page, or anything.

---------------------------------------------------------------------------

2) Cookies are tricky to get into. However, once you get the hang of things, it is very simple.

It is important to keep in mind that passing the $expires parameter will make the cookie expire at that date and time. So, you can manipulate that time itself quite easily. So, as you appear to want the cookies to expire at a certain point in time after they are set, some simple math may be used in conjunction with the time() function. Observe:
Code:
<?php

// There are 60 seconds in a minute, and 60 minutes in an hour.
// Therefore, there are 3600 seconds in a minute.
$hours = 4; // You can adjust this to how many hours you want the cookie(s) to last.
$exp = time() + 3600 * $hours;

setcookie($cookname, $cookvalue, $exp, '/');

?>


The thing to remember, is that time() returns a timestamp. That is, it consists of the number of seconds passed since January 1, 1970 00:00:00 GMT. So multiplying it by anything could bring some...unexpected results.

-----------------------

If it's a login system you need, you can PM me anytime. I'd be more than happy to help you out. =D
ocalhoun
I'll definitely try using output buffers, which will simplify this process eliminating the need for a second file.

Other than that, I'll try using a more reasonable expiration time, something less than 120 years in the future. (basically I was aiming for a cookie that was, for practical purposes, permanent. Could anybody tell me if there is a maximum lifespan for a cookie, and if so what is it? I think the reason they aren't going through might be because of the extremely long expiration period.)
MrBlueSky
ocalhoun wrote:
I'll definitely try using output buffers, which will simplify this process eliminating the need for a second file.

Other than that, I'll try using a more reasonable expiration time, something less than 120 years in the future. (basically I was aiming for a cookie that was, for practical purposes, permanent. Could anybody tell me if there is a maximum lifespan for a cookie, and if so what is it? I think the reason they aren't going through might be because of the extremely long expiration period.)


There is no official maximum lifespan for a cookie, which basically means that a cookie with an expiration time of 120 years in the future only expires if the cookie is deleted by your script (by overwriting it, or changing the expiration date to the past) or by the client (for example, if the user deletes all his cookies, or the browser deletes cookies after specified period of time).
Related topics
PHP include code problem
PHP homepage recreation problem
Preventing bad PHP script calls
Coppermine Problem
Problem with PHP
PHP Form Sending problem
embedding javascript menu simple problem
Php while array problem
PHP login script problem
PHP Form Processing Problem
PHP imagecreate + CSS problem.
PHP imagecreate + CSS problem.
Please can anyone help with a php script error/problem?
PHP Cookies Not Working
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.