Http - Https redirect issue

Discussion in 'PHP' started by mcdeere02, Aug 7, 2009.

Thread Status:
Not open for further replies.
  1. #1
    I have certain pages on my website that have SSL enabled and others that don't need it. I used PHP to check what page the user is on and to see if it needs SSL or not. Here is the function:

    PHP Code:


    function redirectToHTTPS() {

    $url = substr($_SERVER['REQUEST_URI'],1);

    $https = array("page1.php","page2.php","etc...");

    if(in_array($url,$https) && $_SERVER['HTTPS']!="on") {

    $redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

    header("Location:$redirect");

    } elseif(!in_array($url,$https) && $_SERVER['HTTPS']=="on") {

    $redirect= "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

    header("Location:$redirect");

    }

    }



    The problem I am having here is with form processing. If a user processes a form (ie submits data), it submits, then redirects and outputs the wrong message and it gets confusing. Is there a way to do this other than a redirect or maybe though htaccess? Thanks in advance!
    Todd88 is offline Reply With Quote
     
    mcdeere02, Aug 7, 2009 IP
  2. oop

    oop Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    function redirectToHTTPS() {
        $url = substr($_SERVER['REQUEST_URI'],1);
        $https = array("page1.php","page2.php","etc...");
    
        if(in_array($url,$https) && $_SERVER['HTTPS']!="on") {
            $redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            header("Location: $redirect");
        }elseif(!in_array($url,$https) && $_SERVER['HTTPS']=="on") {
           $redirect= "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
           header("Location: $redirect");
        }
    } 
    
    PHP:
    just added spaces between the Location: $var
     
    oop, Aug 7, 2009 IP
Thread Status:
Not open for further replies.