phpretard Posted March 17, 2009 Share Posted March 17, 2009 Is there a time redirect I can code that will (in theory) set a cookie or session when a visitor hits my site and if they sit dormant too long will redirect them to another site (of mine)? <?php $Intial=time(); $HowLong=???; $_SESSION['BeatIT']=$Intial; if ($_SESSION['BeatIT'] < "$HowLong"){ // redirect here } ?> Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/149858-solved-get-off-my-website/ Share on other sites More sharing options...
jhlove Posted March 17, 2009 Share Posted March 17, 2009 if ($_SESSION['BeatIT'] < "$HowLong"){ // redirect here header("location: url "); } Quote Link to comment https://forums.phpfreaks.com/topic/149858-solved-get-off-my-website/#findComment-786979 Share on other sites More sharing options...
ngreenwood6 Posted March 17, 2009 Share Posted March 17, 2009 You could set a cookie with a timestamp. On every page you could check that cookies time with the current time and if it has been more than 5 minutes redirect to another page. Quote Link to comment https://forums.phpfreaks.com/topic/149858-solved-get-off-my-website/#findComment-786980 Share on other sites More sharing options...
rhodesa Posted March 17, 2009 Share Posted March 17, 2009 You can keep the value in the session, but if they just sit there, without navigating anywhere, it won't do anything. if you want to change it without there interaction, you will need to use JavaScript and setTimeout() Quote Link to comment https://forums.phpfreaks.com/topic/149858-solved-get-off-my-website/#findComment-786981 Share on other sites More sharing options...
ngreenwood6 Posted March 17, 2009 Share Posted March 17, 2009 if ($_SESSION['BeatIT'] < "$HowLong"){ // redirect here header("location: url "); } This isnt going to work because that would always be true. For instance if(1:00 < 2:00) because if you set the current time in a session value and check if it is less than the current time it alwasy will be there would have to be more to it than just that edit: oh yeah and you couldnt compare that to minutes Quote Link to comment https://forums.phpfreaks.com/topic/149858-solved-get-off-my-website/#findComment-786984 Share on other sites More sharing options...
iarp Posted March 17, 2009 Share Posted March 17, 2009 JavaScript or Cron job, php alone can't do what you want as it requires the user the do something in order to update its time. JavaScript can update time itself and cron jobs run at timed intervals. Quote Link to comment https://forums.phpfreaks.com/topic/149858-solved-get-off-my-website/#findComment-786985 Share on other sites More sharing options...
phpretard Posted March 17, 2009 Author Share Posted March 17, 2009 I know how and javascript would do fine I supose. Any pointers on the Cron? Would I set a time() value in a DB? Quote Link to comment https://forums.phpfreaks.com/topic/149858-solved-get-off-my-website/#findComment-786997 Share on other sites More sharing options...
iarp Posted March 17, 2009 Share Posted March 17, 2009 Set both the maximum time allowed and the last-done-something-on-a-page(like NOW()) time in another session variable (to save mysql resources if you want) then just compare those. Quote Link to comment https://forums.phpfreaks.com/topic/149858-solved-get-off-my-website/#findComment-787001 Share on other sites More sharing options...
benphelps Posted March 17, 2009 Share Posted March 17, 2009 Untested! <? $initial_visit = time(); $whentoleave = $initial_visit+(60*60); // one hour ? change this to what you want if(!isset($_COOKIE['firstvisit'])){ // set the cookie setcookie('firstvisit', $initial_visit, $whentoleave); } else { // check the cookie $first_visit = $_COOKIE['firstvisit']; if($whentoleave <= $first_visit){ header("Location: http://yournextsite.com/"); } else { // they can stay } } // you could change the cookie name, and base64 the value if you // dont want them to know what the cookie being set is, they could // just change the cookie and stay on the site if they knew ?> Quote Link to comment https://forums.phpfreaks.com/topic/149858-solved-get-off-my-website/#findComment-787002 Share on other sites More sharing options...
phpretard Posted March 17, 2009 Author Share Posted March 17, 2009 Thank you all! Quote Link to comment https://forums.phpfreaks.com/topic/149858-solved-get-off-my-website/#findComment-787013 Share on other sites More sharing options...
samshel Posted March 17, 2009 Share Posted March 17, 2009 Cron alone will not work, as even if you find inside the CRON that the user has expired, you can tell the page to redirect only upon refresh.. the best solution here is JS. <script> var startDate = new Date(); var startTime = startDate.getTime(); function checkDiff(stTime) { var endDate = new Date(); var endTime = endDate.getTime(); if(endTime - stTime >= 30000) { window.location.href = "http://www.phpfreaks.com";//redirect here } else { setTimeout("checkDiff("+stTime+")", 5000); } } checkDiff(startTime); </script> on test page..... Quote Link to comment https://forums.phpfreaks.com/topic/149858-solved-get-off-my-website/#findComment-787020 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.