Jump to content

Archived

This topic is now archived and is closed to further replies.

edgedata

Simple SSL/HTTP link

Recommended Posts

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:

[code]//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>[/code]

Share this post


Link to post
Share on other sites
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

[code]$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>";[/code]

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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.