-1

The page will not redirect IE9 but works in Chrome and IE 11. The code should show text and an error message when the user clicks the login button without completed credentials. The page sticks to the URL http://xxx.xxx.com/customer.php?action_nm=custlogin and will not respond to the following code.

Is there any known issue that IE9 and below does that causes a redirect not to work in PHP?

    if(!empty($_POST["email"])){
        $_SESSION["Email"] = $_POST["email"];

        redirect("customer.php?action_nm=custlogin&action=xpwd"); 
        exit; 
        }
        else
        {

        redirect("customer.php?action_nm=custlogin&action=nocredentials"); 
        exit;           
        }
    }

this is the funcition below:

function redirect($s){
echo '<script language="JavaScript">
function redir()  { document.location="'.$s.'"; }
console.log("' . $s . '")
redir();

 </script>';
}
Rahil Wazir
  • 9,490
  • 11
  • 38
  • 61
r lo
  • 268
  • 2
  • 13

2 Answers2

1

I think your redirect function is malfunctioning, since you haven't posted it on there, i'm posting my own version.

I use this function for redirecting, haven't had any issues so far.

 function redirect($url) {
        try {
            if (!headers_sent()) {
                @header('Location: ' . $url);
                exit;
            } else
                throw new Exception();
        } catch (Exception $ex) {
            // Headers already sent!! Redirect via Javascript?
            echo '<script type="text/javascript">';
            echo 'window.location.href="' . $url . '";';
            echo '</script>';
            // Javascript disabled, redirect via metatags?
            echo '<noscript>';
            echo '<meta http-equiv="refresh" content="0;url=' . $url . '" />';
            echo '</noscript>';
            exit;
        }
    }
mehmetseckin
  • 2,821
  • 1
  • 27
  • 39
0

In order to have a redirect you must put into the HTTP header of the response the following line:

Location: customer.php?action_nm=custlogin&action=xpwd

Now, to have ANYTHING put into the header you may not have written anything yet to the output buffer, or all you can do is put stuff into the HTTP body.

In other words, whatever you have rendered before that line (even an empty space) will make this method not work.

The best practice for PHP is to open the tag just off the very first byte:

<?php

and never close it in the file.

Now, before any other output you can use the header() function to put that line into the HTTP header. So your page should look like this:

<?php

header('Location: customer.php?action_nm=custlogin&action=xpwd');

It is out of question that this works. If it doesn't it's because somewhere a part of the response script has already filled the output buffer. There is no way around this argument.

pid
  • 10,769
  • 5
  • 33
  • 57
  • again it works for other browsers. I would think your answer relates to all browsers right? – r lo Feb 06 '14 at 20:35
  • Copy and paste those lines into a simple static page and open it with IE9. A last thing that might influence this is your very own browser. If you can, try to open that simple static page with IE9 from another machine. – pid Feb 06 '14 at 20:39