Jump to content


Photo

Simple SSL/HTTP link


  • Please log in to reply
2 replies to this topic

#1 edgedata

edgedata
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 10 September 2006 - 01:28 AM

Hey, I run a phpBB with a few private forums and would like to give my users an easy way to switch between SSL and HTTP. Here's some psuedo-code that will hopefully give you guys an idea of what I mean:

//IIRC, php should have an operator to return each part of the URL. 

create var "Link_to" 
create var "Address" 

if [Protocol] == "http://",  var "Link_to" is assigned the value of "Secure" and var "Address" the value of "https://mysite.com/" 

else 

var "Link_to" is assigned the value of "Unsecure" and var "Address" the value of "http://mysite.com:2006" 

//link should read: 

<a href="{"Address"+"Current path"}">"Link_to"</a>


#2 edgedata

edgedata
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 10 September 2006 - 03:57 AM

Bump.

#3 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 10 September 2006 - 05:06 AM

It does have an operator (well, a function), called parse_url().  I agree though that it can be hard to find php functions sometimes.

Try checking $_SERVER['SCRIPT_URI'] for the the full URL of your script (that's URI, not URL).

So, something like

$url_parts = parse_url($_SERVER['SERVER_URI']);
if ($url_parts['scheme']) == 'http') {
  $link_to = 'secure';
  $address = preg_replace('|^http|', 'https', $_SERVER['SERVER_URI']);
} else {
  $link_to = 'insecure';
  $address = preg_replace('|^https|', 'http', $_SERVER['SERVER_URI']);
}

echo "<a href=\"$address\">$link_to</a>";

The meaning of that preg_replace() is to replace "http" with "https" (or the other way around for the second one), but ONLY at the start (that's why the "^" is there, it means "at the start).  The "|" is just a marker for the start and end of the expression, it has no special meaning.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users