PHP Get Site URL Protocol - http vs https
It is not automatic. Your top function looks ok.
remove http and https from string in php
This worked for me,
$http_referer = str_replace($removeChar, "", "https://example.com/");
How to find the domain is whether HTTP or HTTPS (with or without WWW) using PHP?
You could use cURL
method:
$url_list = ['facebook.com','google.com'];
foreach($url_list as $url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_exec($ch);
$real_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
echo $real_url;//add here your db commands
}
This one take some times because it take the last redirected url. if you only want to check whether its http
or https
you could try this:
$url_list = ['facebook.com','google.com'];
foreach($url_list as $url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);
$real_url = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
echo $real_url;//add here your db commands
}
Replace http or https prefix in string PHP
You can try like:
$test = "https://stackoverflow.com/";
$test = rtrim(preg_replace ('/https|http/','test',$test,1),'/');
echo $test;
How to find out if you're using HTTPS without $_SERVER['HTTPS']
This should always work even when $_SERVER['HTTPS']
is undefined:
function isSecure() {
return
(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|| $_SERVER['SERVER_PORT'] == 443;
}
The code is compatible with IIS.
From the PHP.net documentation and user comments :
Set to a non-empty value if the script was queried through the HTTPS protocol.
Note that when using ISAPI with IIS, the value will be "off" if the request was not made through the HTTPS protocol. (Same behaviour has been reported for IIS7 running PHP as a Fast-CGI application).
Also, Apache 1.x servers (and broken installations) might not have $_SERVER['HTTPS']
defined even if connecting securely. Although not guaranteed, connections on port 443 are, by convention, likely using secure sockets, hence the additional port check.
Additional note: if there is a load balancer between the client and your server, this code doesn't test the connection between the client and the load balancer, but the connection between the load balancer and your server. To test the former connection, you would have to test using the HTTP_X_FORWARDED_PROTO
header, but it's much more complex to do; see latest comments below this answer.
Related Topics
Url Encode Equivalent in Ruby on Rails
How to Write to Error Log File in PHP
Wordpress Plugin -> Call to Undefined Function Wp_Get_Current_User()
Differencebetween ' and " in PHP
Laravel 5.3 Withcount() Nested Relation
SQL & PHP - Which Is Faster MySQL_Num_Rows() or 'Select Count()'
Utf8_(En|De)Code Removed from PHP7
MySQL or PHP Is Appending a  Whenever the £ Is Used
Escaping Column Names in Pdo Statements
How to Execute Shell Commands Synchronously in PHP
Symfony2: How to Access Service from Template
Array_Unique and Then Renumbering Keys
Can a User Alter the Value of $_Session in PHP
Check If String Contains Word in Array
Mysql/SQL Retrieve First 40 Characters of a Text Field
Where to Put the PHP Artisan Migrate Command