php - I need help fixing this code -


so, made php page/link checker, should not allow user visit/redirect page if isn't passed minutes last visit/redirect.

the problem is, user being redirected page if did 1 min ago , timer 7 min (example). timer setted mysql minutes.

can't figure out wrong in code

this first page:

<?php session_start();  $sql = "select * table_records";  $result = mysql_query($sql);  $records = array(); while ($row = mysql_fetch_assoc($result)) { $records[] = $row; } foreach ($records $record) { $now = new datetime(); if (!array_key_exists($record, $_session['records']) || ($now->gettimestamp()-$_session['records'][$record]) <= 600) {     echo "<td><center>".$record['id']."</center></td>";     echo "<td><center>".$record['name']."</center></td>";     echo "<td><center>".$record['link']."</center></td>";     echo "<td><center>".$record['delay']."</center></td>";` } else {     // link disabled } } ?> 

and page users redirected to, check timer, , in case redirect them link.

$waiting_time = $delay * 60; //calculate delay time in seconds  if (!array_key_exists($id, $_session['records'])) {  $_session['records'][$id] = $now->gettimestamp(); header("location: $link"); exit();  } elseif (array_key_exists($id, $_session['records']) && ($now->gettimestamp()-$_session['records'][$id]) >= $waiting_time) {  echo "looks visited page";  } elseif (array_key_exists($id, $_session['records']) && ($now->gettimestamp()-$_session['records'][$id]) < $waiting_time) {  $_session['records'][$id] = $now->gettimestamp(); header("location: $link"); exit();  } 

the problem is, user being redirected $link always, if visited, , time of delay isn't passed.

what wrong code?

dry, can write if/elseif statements easier:

if (array_key_exists($id, $_session['records']) && ($now->gettimestamp()-$_session['records'][$id]) < $waiting_time) {     echo "looks visited page"; } else {     $_session['records'][$id] = $now->gettimestamp();     header("location: $link");     exit(); } 

now, if @ you'll see there 2 things check @ first:

  1. is $_session['records'] not empty (maybe session wasn't intialized on second page?) - var_dump ($_session['records']) - what's in there?
  2. what's result of ($now->gettimestamp()-$_session['records'][$id]) , what's in $waiting_time variable - var_dump

don't forget call exit() after dumping code , before redirection or comment location () lines, otherwise you'll see nothing

third possibility (you'll know case if don't see var_dump printout) browser remembers 301 redirection , when go second time same address redirects automatically without calling script - restart browser or try different one.


Comments