22

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);
Sign up to request clarification or add additional context in comments.

3 Comments

good solution, but it is better for incase-sensitive: $url = preg_replace("/^http:/i", "https:", $url);
i tested this solution, unfortunatly this was not working, i tried next solution with "str_replace" and get the result.
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.
16
$url = str_replace( 'http://', 'https://', $url );

Comments

2

One way:

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

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

$url = sprintf($url, $protocol);

1 Comment

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?
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>

Comments

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).

Comments

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'] ));
}

Comments

-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 Comment

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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.