Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: HTTPS in PHP

  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    346
    Thanks
    38
    Thanked 0 Times in 0 Posts

    HTTPS in PHP

    I want to make the link of some of my pages to HTTPS. I am not using SSL certificate because the website is not going to be hosted. I searched a bit on the web to know if we can use HTTPS without SSL certificate in PHP and I have found these coses below:

    PHP Code:
    <?php
    function redirectTohttps()
    {
    if(
    $_SERVER['HTTPS']!=”on”)
    {
    $redirect“https://”.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    header(“Location:$redirect”);
    }
    }
    ?>
    The codes work perfectly, howeve all the pages are turning to HTTPS. I only want some pages to be, so can anyone tell me how to do it with some pages, instead of all?

  • #2
    Regular Coder
    Join Date
    May 2009
    Location
    Moore, OK
    Posts
    282
    Thanks
    11
    Thanked 41 Times in 41 Posts
    Do not call the redirect function on the pages that you do not want to redirect to https://

    Basically when you are calling that function it is checking to see if the https:// is turn on and if not redirects you to the same page with the https:// setting and not the normal http://. So by running the function on non secure pages it forces the SSL to become active... This should be called when you have any data that you do not want sent over unencrypted and will not fire unless told so.

    Why and how are you using the https:// without the SSL certificate? Are you just running through that port? That just defeats the purpose of using https and doesn't make a lot of sense. Are you running it locally where you have a development certificate running or something?
    Last edited by Coyote6; 04-09-2012 at 05:38 AM.

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    346
    Thanks
    38
    Thanked 0 Times in 0 Posts
    i mentioned above, the website will not be hosted...it is just for learning...

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Posts
    346
    Thanks
    38
    Thanked 0 Times in 0 Posts
    I am calling it only on ne page, but it is redirecting all to HTTPS

  • #5
    Regular Coder
    Join Date
    May 2009
    Location
    Moore, OK
    Posts
    282
    Thanks
    11
    Thanked 41 Times in 41 Posts
    The function has to be called in order for it to redirect. Make sure that where you are calling the function it is not a shared resource with the other pages. For instance in the header of all the pages or some other common file. I can't really tell without seeing the actual scripts that you are calling.

  • #6
    Regular Coder
    Join Date
    Sep 2011
    Posts
    346
    Thanks
    38
    Thanked 0 Times in 0 Posts
    I just insert the codes above in my script, then I echo the function like this: echo redirectTohttps(); Thats all...

  • #7
    Regular Coder
    Join Date
    May 2009
    Location
    Moore, OK
    Posts
    282
    Thanks
    11
    Thanked 41 Times in 41 Posts
    Well that still depends on where you call it...

    Say for instance it is in an included file name 'include.php'. That file is then called by your pages.

    include.php
    PHP Code:
    // Function to redirect
    function redirectTohttps() { 
      if(
    $_SERVER['HTTPS']!=”on”) { 
        
    $redirect“https://”.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 
        
    header(“Location:$redirect”); 
      } 


    // Call the redirect function.
    echo redirectTohttps(); 

    index.php
    PHP Code:
    include 'include.php';

    // Do something with the rest of the page. 
    page-1.php
    PHP Code:
    include 'include.php';

    // Do something with the rest of the page. 
    If this is the case then the call is going to all of the pages that include the script. But if you move the call to the individual script pages it will only redirect on the ones calling it.

    include.php
    PHP Code:
    // Function to redirect
    function redirectTohttps() { 
      if(
    $_SERVER['HTTPS']!=”on”) { 
        
    $redirect“https://”.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 
        
    header(“Location:$redirect”); 
      } 


    index.php
    PHP Code:
    include 'include.php';

    // Do something with the rest of the page. 
    page-1.php
    PHP Code:
    include 'include.php';

    // Call the redirect function.
    echo redirectTohttps();

    // Do something with the rest of the page. 
    The index page will not redirect because the function is never called while page-1.php does call it so it will redirect. Also you can eliminate the echo statement if you type exit() in your function.

    include.php
    PHP Code:
    // Function to redirect
    function redirectTohttps() { 
      if(
    $_SERVER['HTTPS']!=”on”) { 
        
    $redirect“https://”.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 
        
    header(“Location:$redirect”); 
        exit();
      } 


    index.php
    PHP Code:
    include 'include.php';

    // Do something with the rest of the page. 
    page-1.php
    PHP Code:
    include 'include.php';

    // Call the redirect function.
    redirectTohttps();

    // Do something with the rest of the page. 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •