0

I am trying to build up a script that sends emails to users and pauses 1 hour after x number of emails. But I get 'Page is not redirecting properly', despite the fact when I access the query string manually it works:

cron-wordpress.php?page=1 // works manually
cron-wordpress.php?page=2 // works manually
cron-wordpress.php?page=3 // works manually

Here is the meat of the script:

EDIT SIMPLIFIED THE PROBLEM

cron-wordpress.php

$page = isset($_GET['page']) ? $_GET['page'] : 0;
$divide = 10;
$start = $divide*$page;
$emails = array();
for ($i = 0; $i <= 300; $i++) { // 300 emails in the dbs that means 30 pages $divide = 10
    array_push($emails, $i.'@email.com');
}
$emails = array_slice($emails, $start, $divide);
$url = "http://".$_SERVER['SERVER_NAME'].'/wordpress/blog/cron_test.php';

$count = 0;
foreach ($emails as $email) {
    echo 'Mail send to '.$email.'<br>';
    $count++;
    if (($count % $divide == 0) !== false && $count < 2000) {
        sleep(2);
        $page_nr = $count/$divide + 1;
        header('Location: '.$url.'?page='.$page_nr);
        //die();
        //exit;
    }
}
Adrian
  • 2,273
  • 4
  • 38
  • 78
  • Have you tried converting your `$_GET` variables to integers, rather than string values, before you do `$start`? – rnevius Oct 11 '14 at 11:16
  • 1
    you're doing a common mistake to miss `exit` or `die` after the redirect header. - and for your problem it's not of interest what you actually do in the script. Instead create a new example from scratch which does the redirect loop but simluates action for example with `sleep()` - that's easier to test for you and it also makes the problem in your question more clear. - trivia: http://thedailywtf.com/Articles/WellIntentioned-Destruction.aspx – hakre Oct 11 '14 at 11:18
  • @hakre Managed to simplify the problem and getting same result. – Adrian Oct 11 '14 at 12:17
  • @user3467855: Can you please remove the database cruft from the example code? I'll take a look then. – hakre Oct 11 '14 at 12:20
  • It goes to page 2 and after getting The page isn't redirecting properly – Adrian Oct 11 '14 at 12:20
  • @hakre Actually I can't because it needs to print the results based on the query from sql. Will add the sql in a sec. – Adrian Oct 11 '14 at 12:20
  • no, for the test it does not need to. just assume database is working. put the sleep inside so it takes let's say three seconds till the next redirect. – hakre Oct 11 '14 at 12:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62880/discussion-between-hakre-and-user3467855). – hakre Oct 11 '14 at 12:24

0 Answers0