views:

231

answers:

1

My site currently uses http and https sections based on the data being collected on the site (form data uses https).

On my index page, I have the PHP code at the top:

<?php
    session_start();
    ob_start();
    if( $_SERVER['SERVER_PORT'] == 443) {
        header('Location:http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])); 
        die();
    } 
?>

However, the page will not load and I get a 404 error. Similarly, when i visit the sections with https security using the head code:

<?php
session_start();
ob_start();
    if( $_SERVER['SERVER_PORT'] == 80) {
        header('Location:https://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/'.basename($_SERVER['PHP_SELF'])); 
        die();
    }
?>

The site does not respond AND for some reason creates a double slash when switching from http to https.

Example: http://www.abc.com/, then clicking button which should route to enroll.php shows http://www.abc.com//enroll.php

why the need for the double slash and can anybody help with the 404 errors?

+2  A: 

dirname() won't work on PHP_SELF because that is not necessarily a full directory.

dirname("/enroll.php") will correctly return an empty string, which in turn leads to the double //.

What exactly are you trying to do?

Pekka
Well - I have the 'exact' same formula on another site and it does function (see www.aabbenefits.com - if you click Contact Us, it redirects to https and you'll see the double slash) so not sure why this site malfunctions.I want my non secure pages to stay http whereas the secure pages need https. I am using forms on the secure pages which collect payment and ssn information.
JM4
@J M the point remains that the method you use to build the URL is flawed (whether or not it is the reason for the 404). Have you tried `$_SERVER["REQUEST_URI"]`?
Pekka
not yet - I apologize I am not very advanced so what are you suggesting I replace with $_SERVER["REQUEST_URI"]?When i run a simple 'echo' statement in the middle of the page at question:<?php echo $_SERVER["REQUEST_URI"]; ?>It simply returns as slash /
JM4
Pekka - I think I see what you are saying now. I replaced the following:header('Location:https://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/'.basename($_SERVER['PHP_SELF']));with header('Location:https://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"]);this removed the double slash!I am still unable to figure out this darned 404 error however ;)
JM4
@J M 4 so that persists? Strange. Can you show an exact URL that causes a 404? Can you make a test output of the URL by not outputting it in `header()` but using `echo()` and `die()`?
Pekka
What Pekka's saying is: Use $_SERVER['Request_URI'] instead of dirname($_SERVER['PHP_SELF']).'/'.basename($_SERVER['PHP_SELF'])
grossvogel
!Pekka - I'm a bit hesistant to post the full URl on the board but am literally just running the exact example as above. What is even more strange is when I run the code locally it works - when i upload to server it doesnt (site B - www.aabbenefits.com) does thoughwhen i ran the following echo statement:<?phpsession_start();ob_start(); if( $_SERVER['SERVER_PORT'] == 80) { echo('Location:https://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"]); die(); }?>I returned:Location:https://localhost/designtest/enroll.php
JM4
@grossvogel - thanks I did do that and it removed the double slash. I am now running into issues however with a site 404 which makes no sense because I KNOW the file exists in the directory.If I remove the ob_start() function from the following code, everything works but not if left in.<?phpsession_start();ob_start(); if( $_SERVER['SERVER_PORT'] == 80) { echo('Location:https://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"]); die(); }?>
JM4
@J M I'm off now, one more thing, try putting the `header()` and `die()` commands in front of `session_start()` and `ob_start()`, if possible in the very first line. Good luck. 'night!
Pekka
good lord - my server configuration didn't have SSL enabled - this was the cause of the 404 error. Thanks for all your help! The double slashes have been removed!
JM4