ereptek Posted February 1, 2015 Share Posted February 1, 2015 I am having difficulty getting the header statement to successfully re-direct to another page when running the PHP code on my local machine. It works perfectly on the server / ISP that I am using (1&1). Statements like the following work: header("location: security/login.php"); I have tried to simplify the hole problem down to just one small PHP file: <?php header("location: http://www.example.com/"); ?> I know I can do a workaround using Javascript, but I would like to avoid that as I am using this method in numerous locations in my website PHP. Link to comment Share on other sites More sharing options...
Ingolme Posted February 1, 2015 Share Posted February 1, 2015 Have you checked for error messages? Are you sure PHP is being executed? Link to comment Share on other sites More sharing options...
ereptek Posted February 2, 2015 Author Share Posted February 2, 2015 I do not get any error messages. When I try to execute the code by Previewing in a Browser, the code appears to stop at the header statement as the address bar shows the initial page, but does not advance to www.example.com . Is it possible that there is an error message that I am not seeing? Link to comment Share on other sites More sharing options...
Ingolme Posted February 2, 2015 Share Posted February 2, 2015 Are you running this on Apache? Link to comment Share on other sites More sharing options...
ereptek Posted February 2, 2015 Author Share Posted February 2, 2015 I am not really sure. I am using Microsoft Web Expression 4 on a Window 7 machine. I have XAMPP installed, however I am getting the following from the control panel: 8:45:46 AM [Apache] Problem detected! 8:45:46 AM [Apache] Port 80 in use by "Unable to open process" with PID 4! 8:45:46 AM [Apache] Apache WILL NOT start without the configured ports free! 8:45:46 AM [Apache] You need to uninstall/disable/reconfigure the blocking application 8:45:46 AM [Apache] or reconfigure Apache and the Control Panel to listen on a different port 8:45:46 AM [Apache] Problem detected! 8:45:46 AM [Apache] Port 443 in use by "C:Program Files (x86)SkypePhoneSkype.exe" with PID 6524! 8:45:46 AM [Apache] Apache WILL NOT start without the configured ports free! 8:45:46 AM [Apache] You need to uninstall/disable/reconfigure the blocking application 8:45:46 AM [Apache] or reconfigure Apache and the Control Panel to listen on a different port However, php still seems to be working. When I put an echo 'test'; statement before the re-direct, the browser correctly shows "test" and then stops. Do I need apache to do a re-direct? Link to comment Share on other sites More sharing options...
ereptek Posted February 2, 2015 Author Share Posted February 2, 2015 Well I just tried on one of my other machines switching to WAMP from XAMPP because of the interference with Ports. I had to stop IIS. Apache appears to be running. PHP works because the test echo statement works. Still no redirect to www.example.com. Link to comment Share on other sites More sharing options...
Ingolme Posted February 2, 2015 Share Posted February 2, 2015 Make sure errors are displaying by putting this at the top of the documents: ini_set('display_errors', 1);error_reporting(E_ALL); If you try to send a header after any content has been displayed then PHP will show an error "Headers already sent". Link to comment Share on other sites More sharing options...
ereptek Posted February 2, 2015 Author Share Posted February 2, 2015 Tried it. Both with and without the echo statement. No errors either way. With the echo statement, "test" is displayed. Without, all I get is an empty browser screen. I assume that if i inserted yes statement above correctly and I had the echo 'test'; statement before the header statement that I should have seen the error you mentioned above.. Link to comment Share on other sites More sharing options...
Ingolme Posted February 2, 2015 Share Posted February 2, 2015 Open your browser developer tools, click on the Network tab and then reload the page to see what shows up. Check the response headers and body. Link to comment Share on other sites More sharing options...
ereptek Posted February 2, 2015 Author Share Posted February 2, 2015 Here is the screenshot: http://www.evernote.com/l/AE_eQN6lgZtPE5xiiXBtemGo5943k71Z93k/ Link to comment Share on other sites More sharing options...
Ingolme Posted February 2, 2015 Share Posted February 2, 2015 That's much too small. I can't read anything on it. What you need to do is to make sure that the "Location" header is getting to the browser. What does your entire PHP code look like? I suspect the header isn't getting sent because you have content before it. Link to comment Share on other sites More sharing options...
ereptek Posted February 2, 2015 Author Share Posted February 2, 2015 Here is the entire file: <?php ini_set('display_errors', 1); error_reporting(E_ALL); // echo 'test' ; header("location: http://www.example.com/"); ?> Try this screen shot - hopefully, I have managed to enlarge it sufficiently: http://www.evernote.com/l/AE_eQN6lgZtPE5xiiXBtemGo5943k71Z93k/ Link to comment Share on other sites More sharing options...
justsomeguy Posted February 2, 2015 Share Posted February 2, 2015 That code should never result in a blank page. In your screenshot, why are there so many requests listed? There are requests for Javascript and CSS files, but your code does nothing except send a location header. Link to comment Share on other sites More sharing options...
ereptek Posted February 2, 2015 Author Share Posted February 2, 2015 (edited) I was able to eliminate all of the entries other than the test-redirect.php be disabling Norton Internet Security toolbar add-in, and several other browser add ins. The browser window is blank because in the code above, the echo statement is remarked out (// at the beginning of the line). I refereshed the browser with all of the add-ins disabled. Is there anything in the browser security that might be preventing the re-direct? When I click on the test-redirect.php on the list and then on the header tab I get the following: Remote Address: 127.0.0.1:61633 Request URL: http://localhost:61633/test-redirect.php Request Method: GET Status Code: 200 OK Request Headersview source Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8 Cache-Control: max-age=0 Connection: keep-alive Cookie: PHPSESSID=t70vppv7cisvgqh87ef6l25hu4 Host: localhost:61633 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36 Response Headersview source Connection: Close Content-type: text/html Date: Mon, 02 Feb 2015 21:41:12 GMT location: http://www.example.com/ Server: Microsoft Expression Development Server/4.0.0.0 Status: 302 Moved Temporarily X-Powered-By: PHP/5.5.11 Edited February 2, 2015 by ereptek Link to comment Share on other sites More sharing options...
justsomeguy Posted February 2, 2015 Share Posted February 2, 2015 I don't know, I guess that extensions might be able to intercept or cancel that if they want to. The browser window is blank because in the code above, the echo statement is remarked outIt should be commented out, but you should still be getting redirected and then seeing example.com. In the network requests tab you should be able to click on the request for your page and see the response headers. One of the response headers should be the location header. You're supposed to capitalize all headers but in general most browsers still support lowercase headers. Link to comment Share on other sites More sharing options...
ereptek Posted February 2, 2015 Author Share Posted February 2, 2015 Here are the response headers: [*] [*] Remote Address: 127.0.0.1:61633 [*] Request URL: http://localhost:61633/test-redirect.php [*] Request Method: GET [*] Status Code: 200 OK [*]Request Headersview source [*] Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 [*] Accept-Encoding: gzip, deflate, sdch [*] Accept-Language: en-US,en;q=0.8 [*] Cache-Control: max-age=0 [*] Connection: keep-alive [*] Cookie: PHPSESSID=t70vppv7cisvgqh87ef6l25hu4 [*] Host: localhost:61633 [*] User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36 [*]Response Headersview source [*] Connection: Close [*] Content-type: text/html [*] Date: Mon, 02 Feb 2015 21:41:12 GMT [*] location: http://www.example.com/ [*] Server: Microsoft Expression Development Server/4.0.0.0 [*] Status: 302 Moved Temporarily [*] X-Powered-By: PHP/5.5.11 Link to comment Share on other sites More sharing options...
ereptek Posted February 2, 2015 Author Share Posted February 2, 2015 Sorry about the format above. For some reason when I past it in, it reformats the header response to a numbered list. Link to comment Share on other sites More sharing options...
Ingolme Posted February 2, 2015 Share Posted February 2, 2015 Have you tried changing "location" for "Location"? Link to comment Share on other sites More sharing options...
ereptek Posted February 3, 2015 Author Share Posted February 3, 2015 Tried it (again) - no change. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 3, 2015 Share Posted February 3, 2015 Maybe the server is having problems. Your output above (under #5) shows a status of 200, which would mean no redirect. But under the response headers it shows a status of 302, which is a redirect. Maybe your webserver is ignoring that for some reason and still setting the status to 200 when it should be 302. Link to comment Share on other sites More sharing options...
ereptek Posted February 5, 2015 Author Share Posted February 5, 2015 Frustrating. My other alternative is to use some Javascript to do the re-direct. I assume that there is no other method to do this with PHP only. FYI, this method is used extensively in the security code that I am using. I.e. if the Password has not been entered - switch back to the login screen using header("location: login.php"); Any advice is appreciated. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 5, 2015 Share Posted February 5, 2015 Sending a Location header is the standard way to do a redirect. When you do that the server is telling the browser to redirect by sending an HTTP header, which is normally what you want. The server should send a 302 status code along with a Location header to tell the browser where to go. It's so common that Location is the first example in the manual for the header function:http://php.net/manual/en/function.header.php Link to comment Share on other sites More sharing options...
ereptek Posted February 7, 2015 Author Share Posted February 7, 2015 Thanks for everyone's advice. I decided to see if the problem was related to MS Web Expression 4 rather than the code. I downloaded Adobe Dreamweaver and tried. The php redirect worked perfectly. Then I opened the same file in Web Expression and got no redirect. All other php code works fine locally with Web Expression including phpinfo() so my only conclusion is that there is something wrong with Web Expression or my setup with that tool specifically. Let me know if anyone has ideas. In the meantime, I will be looking into migrating to another platform. Link to comment Share on other sites More sharing options...
smus Posted February 12, 2015 Share Posted February 12, 2015 I am having difficulty getting the header statement to successfully re-direct to another page when running the PHP code on my local machine. It works perfectly on the server / ISP that I am using (1&1). Statements like the following work: header("location: security/login.php"); I have tried to simplify the hole problem down to just one small PHP file: <?php header("location: http://www.example.com/"); ?> I know I can do a workaround using Javascript, but I would like to avoid that as I am using this method in numerous locations in my website PHP. I've had the same. As an alternative, try to use javascript(it works in my code): <script language="JavaScript"> window.location.href = "yourfile.php"</script> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now