21

I am trying to figure out how to add s after HTTP once a user checks a box in the html form.

I have in my PHP,

$url = 'http://google.com';

if(!isset($_POST['https'])) { 
  //something here
}

So basically, when the user checks a box with the name="https" i want to add s to $url's http making it https://google.com.

I have little knowledge on PHP and if someone can explain to me how to go about doing this, this would be really helpful! thanks.

7 Answers 7

82
$url = preg_replace("/^http:/i", "https:", $url);
3
  • 1
    good solution, but it is better for incase-sensitive: $url = preg_replace("/^http:/i", "https:", $url); Commented Apr 22, 2014 at 5:33
  • 1
    i tested this solution, unfortunatly this was not working, i tried next solution with "str_replace" and get the result.
    – Mimouni
    Commented Sep 27, 2016 at 15:09
  • str_replace didnt work for me, but the original did. Also many thanks for this great solution, saved a giant issue with a wordpress theme im customizing that would otherwise force mixed content over SSL and break SSL connection.
    – Cacoon
    Commented May 6, 2018 at 22:22
15
$url = str_replace( 'http://', 'https://', $url );
0
2

One way:

$url = '%s//google.com';
$protocol = 'http:';

if(!isset($_POST['https'])) { 
    $protocol = 'https:';
}

$url = sprintf($url, $protocol);
1
  • thanks for the code but I can't break up the $url because it is being used in a previous function which requires the entire url. Would there be another way of doing it?
    – Kevin Jung
    Commented Mar 13, 2011 at 12:20
2

When you consider case-insensitive, then use str_ireplace function.

Example:

$url = str_ireplace( 'http://', 'https://', $url );

Alternatively, incase you ant to replace URL scheme in CDN files. Example:

$url = str_ireplace( 'http:', 'https:', $url );

This... (with 'https:')

<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

Becomes... ('https:' has been replaced)

<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
1

I don't know on how many pages you want this to happen onward the user checks the box, but one answer is JavaScript and the base tag.

With the base tag, you can force a different origin, what your relative URL-s will be resolved against.

I you are using it ina form, and the user ticks the checkbox them sumbits the form, all other pages will be viewed from the https site, so you can use relative URL-s everywhere, just insert a different base tag when the user wants to change the site form or to http(s).

1

A solution that doesn't replace urls which contain other urls, for instance http://foo.com/redirect-to/http://newfoo.com

$desiredScheme = "http"; // convert to this scheme;
$parsedRedirectUri = parse_url($myCurrentUrl);
if($parsedRedirectUri['scheme'] !== $desiredScheme) {
    $myCurrentUrl= substr_replace($myCurrentUrl, $desiredScheme, 0, strlen( $parsedRedirectUri['scheme'] ));
}
-1
$count = 1;
$url = str_replace("http://", "https://", $url, $count);

Note : Passing 1 directly will throw fatal error (Fatal error: Only variables can be passed by reference) so you have to pass it last parameter by reference.

1
  • 2
    It throws an error because $count will just contain the number of replacements performed. It's not a parameter meant to set boundaries or anything. Commented Mar 13, 2015 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.