bidon
bidon

Reputation: 27

PHP header("Location: $path") works on Chrome/Firefox but doesn't work on Safari

As Title says, The PHP code is perfectly fine in Chrome/Firefox but gives an error in Safari. I also checked the cache problem on safari so it is not because of that but I am still open to suggestions about the cache problem. However, I do not think it is likely to because of the cache problem. I couldn't find a generally accepted answer for this problem online and most of the answers I found didn't work. Anyways, I am using the following code for opening a pdf with PHP using redirection. I get pdf paths from a table and they are correct.

    <?PHP 
    $mish = $_GET['mission'];
    $dbLink = new mysqli('server','user name','password','database name');
      if(mysqli_connect_errno()) {
        die("MySQL connection failed: ". mysqli_connect_error());
      }
    $queryTotal = "SELECT pdf_path FROM table WHERE id = '$mish' ";
    $result=mysqli_query($dbLink, $queryTotal);
      if(mysqli_num_rows($result) != 0) {
        $row = mysqli_fetch_array($result);
        header("Location: ".$row['pdf_path']); **// I believe this line is problematic**
        exit();
      }
    ?>

I checked the $row['pdf_path'] paths and they are correct, they are working on Chrome anyways. When I comment out header line code flows instead of giving an error but of course, opens a blank page.

Safari Can't Open the Page

Safari can't open the page "server_name/php_pdf_reader_with_correct_mission" because the page's address isn't valid.

This is the error I get. I replace the name of the server etc. but the general picture is there. I also tried the first answers I get when I searched the problem online, like adding the javascript redirection line as follows. It didn't work too. But I am not familiar with js I may have a typo or something.

echo '<script>window.location = "'.$row['pdf_path'].'";</script>';
die;

I also tried to add ob_start(); over and there but it didn't change anything.

EDIT:

header('Location: http:' . $_SERVER[HTTP_HOST]. rawurldecode($path));

I tried this code and it redirects and works for safari. HOWEVER, now it is not working on Chrome.

Any help would be appreciated,

Thanks in advance.

Upvotes: 0

Views: 1522

Answers (2)

bidon
bidon

Reputation: 27

I found the solution and wanted to share it from here.

$path = $row['pdf_path'];
$browser = get_browser();
if($browser[browser] == 'Safari') {
    header('Location: http:' . $_SERVER[HTTP_HOST]. rawurldecode($path));
}
else {
    header( "HTTP/1.1 301 Moved Permanently");
    header( "Location: $path" );
    echo '<script>window.location = "'.$path.'";</script>';
    echo 'window.location.replace('.$path.');';
    die;
}

It is not as elegant as I hoping for but It works. I still do not know what is the problem but this worked out for me!

Upvotes: 0

baijugoradia
baijugoradia

Reputation: 150

Try this code, because it contains HTTP response code

<?php
 $url_path = 'public path to file';
 header( "HTTP/1.1 301 Moved Permanently");
 header( "Location: $url_path" );
 // Backup code
 echo '<script>window.location = "'.$url_path.'";</script>';
 echo 'window.location.replace('+$url_path+');';
 die;
?>

Upvotes: -2

Related Questions