471,887 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,887 software developers and data experts.

Enforcing https with javascript

200 100+
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
3 1321
200 100+
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;
  4.     if (url.indexOf("https")==-1)
  5.     {
  6.             //alert('den einai https');
  8.     }
  9.     else
  10.     {
  11.         //alert('einai https');
  12.         url=url.replace(/https/i,"http")
  13.         window.location = url; 
  15.     }
  17. goElseWhere(); 
Note that the script above enforce http if the user types https
Oct 15 '08 #2
16,027 Expert Mod 8TB
Why not use server-side code for this? If someone has JavaScript disabled, it will remain on HTTP only.
Oct 15 '08 #3
392 Expert 256MB
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.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.