0

I am having a problem with php header redirect not working on safari

if (!isset($lang))
{
header('Location: http://domain.com/en/home');
}

I have read that this could be done with JS fairly simple, but no idea how to do it with the condition if $lang is set.

Should, I just execute the js within the "php if", or there is a better/clever way to do it.

Thanks.

3
  • 2
    You are writing a php code which runs on backend (server), and you feel that there is a browser issue for redirection. Please elaborate your question, I don't think there is any issue with browser for php redirection. Commented Oct 14, 2015 at 7:12
  • 1
    You say "Having a problem", but you don't say what that problem is. I simply can't believe that Safari doesn't understand the "Location" header. It's been a part of the HTTP protocol since the late 1980's, so that would be an epic bug! Commented Oct 14, 2015 at 7:27
  • The problem is that the website shows for couple of seconds and then Safari says "A problem repeatedly occurred with website...". I did some search and saw that there is/was a problem with php headers and Safari in the past. So, I have a sumed that this is what my problem was as well. Commented Oct 14, 2015 at 7:37

4 Answers 4

1

First off, you should stop the php execution if you set a redirect header:

if (!isset($lang))
{
     header('Location: http://domain.com/en/home');
     exit;
}

Note: for this to work properly, you need to set this header before any other and before setting any doctype. Also, the exit prevents anything else from being sent to the broswer (content, other headers, etc). Mixing headers can result in unwanted behavior.

Secondly, you can do:

if (!isset($lang))
{
     ?>
     <script>
     window.location = "http://domain.com/en/home";
     </script>
     <?php
}  
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, somehow I missed the exit part. I guess I should stop working night time :) It is fixed now!
1

Using Java Script

if (!isset($lang))
{
  ?>

    <script>
    location.href = "http://domain.com/en/home";
    </script>

  <?php
}

Comments

1

You can try with ob_start()

ob_start()//Define at the top of the php page.
if (!isset($lang))
{
header('Location: http://domain.com/en/home');
exit;
}

Comments

1

You can use echo to print HTML tag <script> and put your JavaScript code inside this tag.

if (!isset($lang)){
   echo "
       <script>
          window.location = "http://domain.com/en/home";
       </script>
    ";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.