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
Cookie Question - Need coding help
Moderator: General Moderators
-
MysticJoe101
- Forum Newbie
- Posts: 1
- Joined: Sun May 04, 2008 1:52 pm
Re: Cookie Question - Need coding help
Hey there, you can set a cookie for as long as you want. It doesn't have to disappear after a year. You should be able to use either format to set the expiration date. However, since not all computers accept cookies I suggest you use sessions instead.
http://us.php.net/manual/en/features.sessions.php
That way you can handle all browsers whether they accept cookies or not.
http://us.php.net/manual/en/features.sessions.php
That way you can handle all browsers whether they accept cookies or not.
-
nowaydown1
- Forum Contributor
- Posts: 169
- Joined: Sun Apr 27, 2008 1:22 am
Re: Cookie Question - Need coding help
MysticJoe101: What Jade said previously might be sort of misleading. 9 times out of 10 if you're using a standard session setup, you'll still be using cookies. PHP will slam a cookie on the browser that contains your session id. So unless you're passing the session ID as a get parameter on every request you'll still be using cookies.
For what it sounds like you're trying to do I would recommend cookies. Here's a really simple example that I think is applicable to your situation:
The above would be placed in your intro.php file that you mentioned. Obviously the markup I added would be removed in place of yours. You can very well tweak the cookie expiration to your liking. Let me know if you have any questions.
For what it sounds like you're trying to do I would recommend cookies. Here's a really simple example that I think is applicable to your situation:
Code: Select all
<?php
if($_COOKIE['visited'] == 1) {
header("Location: main.php");
exit;
} else {
setcookie("visited", 1, time()+3600);
}
?>
<h1>Introduction Page</h1>
Welcome! Next time you won't see this page.