-2

It's the first time i write in this site, even if i've used it an enormous amount of times!

My question is that i need to run a php script before the html loads. I have an index.php, so i can write the php instructions by opening and closing it (<?php ------ ?>). The php that i have will controll a variable value and if it's false it will instantly reload the page and redirect to another. It works but if I press the "X" button on top (the one that becomes a reload button) the script will be "bypassed" and it will show the whole html page i have.

The file.php with the html is like this:

<?php ----------
----------------
header("refresh:0; url=redirect.html");
?>

<html>
<head>
----------
----------
</head>
</html>

Is there something else i can try to do?

Johannes
  • 64,305
  • 18
  • 73
  • 130
  • 5
    Don't use JavaScript or Meta Tags to redirect a web page. Use [`Location: header`](//stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php) instead. Check [PHP the Right Way](//phptherightway.com/) for more up-to-date advice. – John Conde Jan 16 '18 at 20:26
  • well ..when you need to redirect use header('location: where?'); die(); http://php.net/manual/en/function.header.php when you need to skip showing that whole html do not user header but die(); –  Jan 16 '18 at 20:30

2 Answers2

2

You need to exit the code, like this (Here I used Location):

header("Location: https://www.example.com");
exit;

exit will prevent PHP from executing the code after where it exists.

Spoody
  • 2,852
  • 1
  • 26
  • 36
  • reading the manual i agree with this answer http://php.net/manual/en/function.header.php BUT USING MY EXPERIENCE I USE header("Location: https://www.example.com");DIE(); The idea is quite simple : while php should still calculate where the script exit and if is an loop or not the die(); function stops executions then php redirect that hedear he send first. –  Jan 16 '18 at 20:55
  • 2
    @Constantin I'm not sure what you mean, `exit` is the same as `die`. – Spoody Apr 14 '18 at 16:19
  • relative to humen eyes YES ISN'T ANY KIND OF DIFFERENCES.web php is a script language so interpreted ,where calling function die() the interpreter will don nothing else to close the "execution" of current script .If you check php manual for the function "exit" (Description ¶ void exit ([ string $status ] ) void exit ( int $status ) ) that meaning the interpreter will check something more to resolve any parameters you are using there...that was what i meaning ! lol! –  Apr 15 '18 at 17:04
  • 3
    @Constantin I still have no idea what you are talking about.... `die` is **exactly** the same as `exit`, there is literally no difference... – Spoody Apr 15 '18 at 17:11
  • nope isn't ...they has similarity aren't the same, see the manual first and try to understant precisely what you read! –  Apr 17 '18 at 09:02
1
<?php
 header('Location: redirect.html');
 exit; 
Muhammad Usman
  • 1,403
  • 13
  • 24
  • absolute URI recomended –  Jan 16 '18 at 20:29
  • 1
    @rtfm According to [this answer](https://stackoverflow.com/a/25643550/4875631) relative URL's are permitted. No reason to do absolute if you don't need to. – Blue Jan 16 '18 at 20:31
  • acordding to the MANUAL "Note: Most contemporary clients accept relative URIs as argument to » Location:, but some older clients require an absolute URI including the scheme, hostname and absolute path." @FrankerZ –  Jan 16 '18 at 20:32
  • 3
    I believe the days of Netscape Navigator 4 are over. – Blue Jan 16 '18 at 20:33
  • i said recommended not compulsory the rfc specs say its required, you have no way of know if a client will come out in the future that actually follows the specs, so why not code smart? –  Jan 16 '18 at 20:48
  • @Constantin: Could you please remove your duplicate comments? – hakre Apr 15 '18 at 17:19