Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/149858-solved-get-off-my-website/
Share on other sites

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

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

?>

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.....



This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.