By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,030 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,030 IT Pros & Developers. It's quick & easy.

Enforcing https with javascript

mikek12004
100+
P: 200
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?)
Oct 15 '08 #1
Share this Question
Share on Google+
3 Replies


mikek12004
100+
P: 200
After some digging I found this which works, (though I do not know if it will with nice URLS)
Expand|Select|Wrap|Line Numbers
  1. function goElseWhere() 
  2. var url = document.URL;
  3.  
  4.     if (url.indexOf("https")==-1)
  5.     {
  6.             //alert('den einai https');
  7.  
  8.     }
  9.     else
  10.     {
  11.         //alert('einai https');
  12.         url=url.replace(/https/i,"http")
  13.         window.location = url; 
  14.  
  15.     }
  16.  
  17. goElseWhere(); 
  18.  
Note that the script above enforce http if the user types https
Oct 15 '08 #2

acoder
Expert Mod 15k+
P: 16,027
Why not use server-side code for this? If someone has JavaScript disabled, it will remain on HTTP only.
Oct 15 '08 #3

Expert 100+
P: 392
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.
Oct 20 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.