Enforcing https with javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mikek12004
    New Member
    • Sep 2008
    • 200

    Enforcing https with javascript

    For a spesific page (order.php) I want if the user didn't entered https to automatically reload it with https (note that this page loads also other pages with includes based on a _GET variable-e.g order.php?stat= 1) and in all other pages to enforce http (all other pages loads with index.php in which I include all the others based on a _GET variable like above any suggestions?)
  • mikek12004
    New Member
    • Sep 2008
    • 200

    #2
    After some digging I found this which works, (though I do not know if it will with nice URLS)
    Code:
    function goElseWhere() 
    { 
    var url = document.URL;
    	
    	if (url.indexOf("https")==-1)
    	{
    			//alert('den einai https');
    			
    	}
    	else
    	{
    		//alert('einai https');
    		url=url.replace(/https/i,"http")
    		window.location = url; 
    		
    	}
    
    } 
    goElseWhere();
    Note that the script above enforce http if the user types https

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Why not use server-side code for this? If someone has JavaScript disabled, it will remain on HTTP only.

      Comment

      • pronerd
        Recognized Expert Contributor
        • Nov 2006
        • 392

        #4
        It would be both easier and more reliable to do this with the server configuration. You would not have to write and code, and it would still work if the user had JavaScript disabled.

        Comment

        Working...