0

This is an interesting one... I am trying to test for something, then show an alert then once the OK is clicked relocate the page to somewhere else, using header However if I have the code below it just goes straight to the URL. If I comment out the header then the alert works....

elseif ($user['archived']=="1"){
    echo "<script type='text/javascript'>alert('Sorry you are no longer an owner');</script>";
    sleep(10);
    header('location: https://some URL'); 
}
Rahul
  • 18,271
  • 7
  • 41
  • 60
  • 1
    You are doing a header redirect on the server so that will occur even before the html / javascript makes it to the browser. If you need to execute javascript first, you would need to wait and redirect in javascript as well. – jeroen May 29 '19 at 08:55
  • 1
    Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Dharman May 29 '19 at 08:56
  • Completely get the Server Side, Client Side thing, but dont see how its a duplicate of the article.. Its a question on how to fix it. The article explains the difference. I couldnt figure out why even with the 'sleep' it was still not 'doing' the alert, before it got to the header command. – Jason Ellmers May 29 '19 at 09:48

1 Answers1

1

You could use a meta redirect instead. I'm not sure if this is good or bad practice but it will do the trick for you.

if (...) {
    // ...
} elseif ($user['archived']=="1") {
    echo '<meta http-equiv="refresh" content="10; URL=someurl">';'
    echo "<script type='text/javascript'>alert('Sorry you are no longer an owner');</script>";
    exit;
}
Peter
  • 8,776
  • 6
  • 62
  • 95