PAUSIMS Posted October 11, 2009 Share Posted October 11, 2009 Hi everyone, First of all I should apologise for my level of English, it's not good but anyway I hope you can understand me. I'm absolutely new at Php and I want to create an index.html with a php cookie that if it's the first visit takes you to index2, if you already visited the site takes you to index3. I found this code: <?php $visits = $visits + 1; setCookie("visits",$visits,time() +3600*24*365); ?> <html> <body> <?php If ($visits > 1 { Echo ("HERE NEEDS TO TAKE TO INDEX3"); } else { Echo ("HERE NEEDS TO TAKE TO INDEX2"); } ?> </body> </html> So, I don't know how to complete this code for the redirection where it says "here needs to take to index3" and "here needs to take to index2". Also, if some of you know another code that could be more reliable and well done than the above one would be very appreciate. Thanks in advance, Pau Link to comment https://forums.phpfreaks.com/topic/177289-cookie-with-redirection/ Share on other sites More sharing options...
jon23d Posted October 11, 2009 Share Posted October 11, 2009 If you do not want to rely on the browser redirecting, then just use require_once(), otherwise header("Location: http://mydomain.com/mypage.php"). Headers can only be sent prior to any other output though. Link to comment https://forums.phpfreaks.com/topic/177289-cookie-with-redirection/#findComment-934792 Share on other sites More sharing options...
PAUSIMS Posted October 11, 2009 Author Share Posted October 11, 2009 So I tried to add the require_once() function in the code but I think I made some mistake because it doesn't work. As I said, I'm a completely newbie in Php and I'm very lost, so if someone could edit for me the avobe code adding the require_once() function or any other one that could redirect to an HTML page if it's the first visit of the user or if it's not the first visit, it would be very appreciate. Thanks very much Link to comment https://forums.phpfreaks.com/topic/177289-cookie-with-redirection/#findComment-935001 Share on other sites More sharing options...
jon23d Posted October 11, 2009 Share Posted October 11, 2009 If you have no reason to do a redirect, try this: if ($visits > 1) { require_once('first-visit.php'); } else { require_once('subsequent-visits.php'); } Thats it! Link to comment https://forums.phpfreaks.com/topic/177289-cookie-with-redirection/#findComment-935055 Share on other sites More sharing options...
PAUSIMS Posted October 12, 2009 Author Share Posted October 12, 2009 Ok, thanks! I'll try if it works and I'll let you know. Link to comment https://forums.phpfreaks.com/topic/177289-cookie-with-redirection/#findComment-935325 Share on other sites More sharing options...
PAUSIMS Posted October 12, 2009 Author Share Posted October 12, 2009 OK, the require_once() does work but the cookie seems to not work. Even if it's the first visit it takes me to the page for subsequent visits... I deleted all the cookies of my browser but it still doesn't work. The code is the following: FILE: INDEX.PHP <?php $visits = $visits + 1; setCookie("visits",$visits,time() +3600*24*365); if ($visits > 1) { require_once('first.php'); } else { require_once('subseq.php'); } ?> FILE: FIRST.PHP <?php header( 'Location: http://www.mysite.com/welcome.html ) ; ?> FILE: SUBSEQ.PHP <?php header( 'Location: http://www.mysite.com/web' ) ; ?> Link to comment https://forums.phpfreaks.com/topic/177289-cookie-with-redirection/#findComment-935343 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.