Cookie Question - Need coding help
Posted: Sun May 04, 2008 2:06 pm
Folks,
I'm not exactly sure what is the best way to accomplish this task.
I've somehow inherited an older site that I've been updating via CS3. Apache, MySql & PHP are all installed.
Here is the question:
I have a page that I want all first time visitors to view "intro.php". On subsequent visits, I want everyone to go to my main page "main.php". What is the best way to accomplish this? Please "be gentle, this is my first time doing this", so any extra code is appreciated.
As a note I posted this exact question on another forum (I'm so sorry) and essentially received 10 replies saying it can't be done.
Here was my last post to that other forum and never received a reply. Can someone here help me put it together so it'll work.
~~~~~~~~~~~~~~~ post from other forum BELOW ~~~~~~~~~~~~~~~~~
I guess then I'm a bit confused... In a few tutorials I've seen scripts like the one I've copied and pasted below...
Excerpt from --> http://devzone.zend.com/article/646-PHP ... Cookie-Jar ~~~~
PHP offers a single function for cookie manipulation: setcookie(). This function allows you to read and write cookie files, as demonstrated in the following example:
<?php
if (!isset($_COOKIE['visited'])) {
// if a cookie does not exist
// set it
setcookie("visited", "1", mktime()+86400, "/") or die("Could not set cookie");
echo "This is your first visit here today.";
}
else {
// if a cookie already exists
echo "Nice to see you again, old friend!";
}
?>
In the PHP manual it says you can set the expire time for a cookie to as long as 360 days (1 year):
Expire Parameter defination from the PHP manual:
The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).
Note: You may notice the expire parameter takes on a Unix timestamp, as opposed to the date format Wdy, DD-Mon-YYYY HH:MM:SS GMT, this is because PHP does this conversion internally.
expire is compared to the client's time which can differ from server's time.
So now I'm thoroughly confused. Because your in direct conflict with the PHP manual.
My take from the example code and the PHP manual is I could combine the two... I could create a cookie on the client, and set it to last, lets say 90 days. Then see if that cookie was present. If it is then re-direct to main.php. If not, is new user, create cookie and show intro.php.
What I was hoping to get was some help with coding that. However this thread seems to have taken a different line and is of the opinion that what the PHP manual say can be done, actually cannot be done. Which is true? That is where I'm confused.
~~~~~~~~~~~~~ Post from other forum ABOVE ~~~~~~~~~
If it can be done as I've read from the PHP manual then can someone help me with the coding of it?
Thanks to all
I'm not exactly sure what is the best way to accomplish this task.
I've somehow inherited an older site that I've been updating via CS3. Apache, MySql & PHP are all installed.
Here is the question:
I have a page that I want all first time visitors to view "intro.php". On subsequent visits, I want everyone to go to my main page "main.php". What is the best way to accomplish this? Please "be gentle, this is my first time doing this", so any extra code is appreciated.
As a note I posted this exact question on another forum (I'm so sorry) and essentially received 10 replies saying it can't be done.
Here was my last post to that other forum and never received a reply. Can someone here help me put it together so it'll work.
~~~~~~~~~~~~~~~ post from other forum BELOW ~~~~~~~~~~~~~~~~~
I guess then I'm a bit confused... In a few tutorials I've seen scripts like the one I've copied and pasted below...
Excerpt from --> http://devzone.zend.com/article/646-PHP ... Cookie-Jar ~~~~
PHP offers a single function for cookie manipulation: setcookie(). This function allows you to read and write cookie files, as demonstrated in the following example:
<?php
if (!isset($_COOKIE['visited'])) {
// if a cookie does not exist
// set it
setcookie("visited", "1", mktime()+86400, "/") or die("Could not set cookie");
echo "This is your first visit here today.";
}
else {
// if a cookie already exists
echo "Nice to see you again, old friend!";
}
?>
In the PHP manual it says you can set the expire time for a cookie to as long as 360 days (1 year):
Expire Parameter defination from the PHP manual:
The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).
Note: You may notice the expire parameter takes on a Unix timestamp, as opposed to the date format Wdy, DD-Mon-YYYY HH:MM:SS GMT, this is because PHP does this conversion internally.
expire is compared to the client's time which can differ from server's time.
So now I'm thoroughly confused. Because your in direct conflict with the PHP manual.
My take from the example code and the PHP manual is I could combine the two... I could create a cookie on the client, and set it to last, lets say 90 days. Then see if that cookie was present. If it is then re-direct to main.php. If not, is new user, create cookie and show intro.php.
What I was hoping to get was some help with coding that. However this thread seems to have taken a different line and is of the opinion that what the PHP manual say can be done, actually cannot be done. Which is true? That is where I'm confused.
~~~~~~~~~~~~~ Post from other forum ABOVE ~~~~~~~~~
If it can be done as I've read from the PHP manual then can someone help me with the coding of it?
Thanks to all