Jump to content

Simple SSL/HTTP link


edgedata

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]
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.