MysticJoe101 Posted May 4, 2008 Share Posted May 4, 2008 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 with CS3. I have a page that I want all first time visitors to my site to view. On subsequent visits, I want everyone to go to my main page. What is the best way to accomplish this? Please "be gentle, this is my first time doing this", so any extra code is appreciated. After I have that completed... I want all users to go to my main page, even if they've bookmarked something else. How can I accomplish that without effecting my spry menu (included with CS3)? Thanks Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 4, 2008 Share Posted May 4, 2008 There's no real way to make sure only first time visitors view a certain page because IP addresses can change constantly. I think AOL customers use a different IP on each request. And the "go back to a main page even if they've bookmarked something else" is so ridiculously inconvenient that it's not even funny. Why would you want to do that? Quote Link to comment Share on other sites More sharing options...
MysticJoe101 Posted May 4, 2008 Author Share Posted May 4, 2008 Well I thought cookies would be one way to accomplish this. But not too sure if that's the best or only way? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 4, 2008 Share Posted May 4, 2008 To accomplish what? The first objective or the second one? Quote Link to comment Share on other sites More sharing options...
Cless Posted May 4, 2008 Share Posted May 4, 2008 As a solution to the first issue: <?PHP //Define Variables $IP= $_SERVER['REMOTE_ADDR']; //Substr Replace Variables $IP2= substr_replace($IP, "", 6); //Find Rows $Result1= mysql_query("SELECT * FROM Views WHERE IP LIKE '$IP2%'"); $Rows1= mysql_fetch_array($Result1); //If Rows Are Empty if(empty($Rows1)) { //Insert View Data mysql_query("INSERT INTO Views (IP) VALUES ('$IP')"); //Redirect User header("location: intropage.php"); } //If Rows Are Not Empty else { //Redirect User header("location: mainpage.php"); } ?> You could do something like that... Also, the table 'Views' would only have one field: IP. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 4, 2008 Share Posted May 4, 2008 Their IP can change constantly so it's not a good identifier... =/ Quote Link to comment Share on other sites More sharing options...
MysticJoe101 Posted May 4, 2008 Author Share Posted May 4, 2008 Either. I thought I could check if a cookie exists then not a first time visitor and re-direct to main page, but I don't know. And I'm not sure how to code for that either. However that's why I'm here looking to the more experienced folks for answers/solutions/suggestions. Quote Link to comment Share on other sites More sharing options...
Cless Posted May 4, 2008 Share Posted May 4, 2008 That's correct, however it is most likely the closest you could get to. Besides, most IPs only change the last few numbers... the substr replace will only find the first few. Otherwise, you could use this method combined with cookies and/or sessions. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 4, 2008 Share Posted May 4, 2008 @Cless: So then ANYONE with the same first few numbers (which could be a LOT of people) wouldn't be "first time visitors" either. @MysticJoe: I'd use sessions for the second one, but there's no real way to determine if they're first time visitors...cookies expire. Quote Link to comment Share on other sites More sharing options...
MysticJoe101 Posted May 4, 2008 Author Share Posted May 4, 2008 Hey Cless, Thanks for that script I'll print and review so I can learn it but... However, not exactly a pro here... What exactly is it doing? Do I need a MySQL database? What tables? How long will the data stay? Quote Link to comment Share on other sites More sharing options...
Cless Posted May 4, 2008 Share Posted May 4, 2008 No, most users have different numbers at the start. The chances of them having the same are EXTREMELY low. The first six numbers are most often different than those of other individuals. Um, it is checking from a database whether any user with an identical IP address has been on your website. And yes, you will need a database with the table 'Views'. In the table 'Views', you only need one field: IP varchar(15). Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 4, 2008 Share Posted May 4, 2008 I really wouldn't recommend Cless' script....It's just randomly cutting off 6 digits from the end of the IP...You should probably do (if you want to go that route, although I don't suggest it either): $ip = explode(".", $_SERVER['REMOTE_ADDR']); $ip2 = $ip[0] . "." . $ip[1]; >_> Quote Link to comment Share on other sites More sharing options...
Cless Posted May 4, 2008 Share Posted May 4, 2008 The script uses a 'LIKE' SQL method which will find any IPs similar. Of course, it's not dead-accurate, however it is probably one of your only options. There's probably no actual method that will provide a 100% accurate result... so yeah. Quote Link to comment Share on other sites More sharing options...
MysticJoe101 Posted May 4, 2008 Author Share Posted May 4, 2008 Cless/Darkwater, Thanks to both of you for the guidance. So the conclusion is the IP script is not recommended and won't work accurately. Then my question is can I place a file or cookie on the client PC. Then look for that? That would be accurate as long as they don't clean up. I'm willing to live with that. Can anyone advise. If so how? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 4, 2008 Share Posted May 4, 2008 Cookies only last so long....What you COULD do is use Cless' method but with a bit of a twist: If the IP is not in the table, do something like It seems like you've never visited our site! Click here for a tour! Or something like that, but don't FORCEFULLY redirect them...it's kind of endlessly annoying for subsequent visitors. =P Quote Link to comment Share on other sites More sharing options...
MysticJoe101 Posted May 4, 2008 Author Share Posted May 4, 2008 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-101-part-10-A-Session-In-The-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, 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. 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 Quote Link to comment Share on other sites More sharing options...
MysticJoe101 Posted May 4, 2008 Author Share Posted May 4, 2008 Can anyone help me blend this together Quote Link to comment Share on other sites More sharing options...
rarebit Posted May 4, 2008 Share Posted May 4, 2008 Without them registering then yes you definitely need to use cookies (not session cookies), the code you've just shown could be modified like this... <?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 setcookie("visited", "1", mktime()+86400, "/") or die("Could not set cookie"); echo "Nice to see you again, old friend!"; } ?> Which just updates the cookie... p.s. I rarely use cookies but I believe you just reset the cookie... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.