I want to check whether the url is via https or http, but i tested on https://www.techinasia.com/, it return me "http://" instead.
$url = http://example.com;
if($html = #DOMDocument::loadHTML(file_get_contents($url))) {
...
...
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off')
{
echo "https://";
}
else
{
echo "http://";
}
}
Any idea what's wrong?
Related
I am making a json validation that needs to validate url that starts with http:// or https://
if(preg_match("/^[http://][a-zA-Z -]+$/", $_POST["url"]) === 0)
if(preg_match("/^[https://][a-zA-Z -]+$/", $_POST["url"]) === 0)
Am I wrong in synatx, and also how should i combine both (http and https) in same statement ?
Thank you !
Use $_SERVER['HTTPS']
$isHttps = (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) ? true : false;
you can use parse_url
<?php
$url = parse_url($_POST["url"]);
if($url['scheme'] == 'https'){
// is https;
}else if($url['scheme'] == 'http'){
// is http;
}
// try to do this, so you can know what is the $url contains
echo '<pre>';print_r($url);echo '</pre>';
?>
OR
<?php
if (substr($_POST["url"], 0, 7) == "http://")
$res = "http";
if (substr($_POST["url"], 0, 8) == "https://")
$res = "https";
?>
If you want to check your string starts with http:// or https:// without worrying about the validity of the whole URL, just do that :
<?php
if (preg_match('`^https?://.+`i', $_POST['url'])) {
// $_POST['url'] starts with http:// or https://
}
Is there anyone know how to achieve this? , if possible i don't want the full url , i just want to get whether it's http or https in laravel or php .
use parse_url()
Parse a URL and return its components
$url = 'http://username:password#hostname:9090/path?arg=value#anchor';
echo parse_url($url, PHP_URL_SCHEME);
// var_dump(parse_url($url)); it will return all components .
OUTPUT :
http
You can use Laravels built in Request secure() method.
You can do it like so in php:
if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443) {
return true;
}
You can wrap it in a function like so:
function requestIsHTTPS(){
if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443) {
return true;
}
return false;
}
I need to do something based on either http or https. For example:
$https = strpos($_SERVER['REQUEST_URI'], 'https') !== false;
if ($https) { ?>
<script type="text/javascript">
var a = 'a';
</script>
<?php } else { ?>
<script type="text/javascript">
var b = 'b';
</script>
<?php } ?>
But this doesn't work. It always goes to the second option regardless the page I access is http or https. Is there any work around? Thanks in advance.
Try using if (isset($_SERVER['HTTPS'])) instead.
$_SERVER['REQUEST_URI'] only returns the path relative to the server root, not the domain or protocol. Besides, your code would fail if I were to access http://example.com/https-is-cool.
Instead, try:
$https = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== "off";
if(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443){
echo 'https';
}else{
echo 'http';
}
or as a variable:
$https = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? true : false;
I would agree with #MichaelBertwoski's comment that if you are simply trying to do this for javascript, do the detection in javascript like this.
if (window.location.protocol == 'https:') {
var a = 'a';
} else {
var b = 'b';
}
If you need the information in PHP you can use one of the other answers posted.
In PHP, is there a reliable and good way of getting these things:
Protocol: i.e. http or https
Servername: e.g. localhost
Portnumber: e.g. 8080
I can get the server name using $_SERVER['SERVER_NAME'].
I can kind of get the protocol but I don't think it's perfect:
if(strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https') {
return "https";
}
else {
return "http";
}
I don't know how to get the port number though. The port numbers I am using are not 80.. they are 8080 and 8888.
Thank you.
Have a look at the documentation.
You want $_SERVER['SERVER_PORT'] I think.
The function that returns the full protocol-server-port info:
function getMyUrl()
{
$protocol = (!empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == 'on' || $_SERVER['HTTPS'] == '1')) ? 'https://' : 'http://';
$server = $_SERVER['SERVER_NAME'];
$port = $_SERVER['SERVER_PORT'] ? ':'.$_SERVER['SERVER_PORT'] : '';
return $protocol.$server.$port;
}
$_SERVER['SERVER_PORT'] will give you the port currently used.
Here's what I use:
function my_server_url()
{
$server_name = $_SERVER['SERVER_NAME'];
if (!in_array($_SERVER['SERVER_PORT'], [80, 443])) {
$port = ":$_SERVER[SERVER_PORT]";
} else {
$port = '';
}
if (!empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == 'on' || $_SERVER['HTTPS'] == '1')) {
$scheme = 'https';
} else {
$scheme = 'http';
}
return $scheme.'://'.$server_name.$port;
}
<?php
$services = array('http', 'ftp', 'ssh', 'telnet', 'imap', 'smtp', 'nicname', 'gopher', 'finger', 'pop3', 'www');
foreach ($services as $service) {
$port = getservbyname($service, 'tcp');
echo $service . ":- " . $port . "<br />\n";
}
?>
This is display all port numbers.
If you already know port number you can do like this,
echo getservbyport(3306, "http"); // 80
$protocol = isset($_SERVER['HTTPS']) && (strcasecmp('off', $_SERVER['HTTPS']) !== 0);
$hostname = $_SERVER['SERVER_ADDR'];
$port = $_SERVER['SERVER_PORT'];
if(strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,4))=='http') {
$strOut = sprintf('http://%s:%d',
$_SERVER['SERVER_ADDR'],
$_SERVER['SERVER_PORT']);
} else {
$strOut = sprintf('https://%s:%d',
$_SERVER['SERVER_ADDR'],
$_SERVER['SERVER_PORT']);
}
return $strOut;
Try something like that if you want
nothing worked serverside , something was wrong on APACHE and I had no access to
the server and I ended up redirecting to http throught Javascript, It's not the ideal solution maybe this can save someone else in my situation
<script>
if(!window.location.href.startsWith('https'))
window.location.href = window.location.href.replace('http','https');
</script>
To get server name and port number just apply this functions
";
// Append the requested resource location to the URL
echo $url .= $_SERVER['REQUEST_URI'];
?>
Why don't you get full url like this
strtolower(array_shift(explode("/",$_SERVER['SERVER_PROTOCOL'])))."://".$_SERVER['SERVER_NAME'];
or (If you want host name from HTTP)
strtolower(array_shift(explode("/",$_SERVER['SERVER_PROTOCOL'])))."://".$_SERVER['HTTP_HOST'];
I have a website in Zend framework. Here I want to identify whether the current URL contains HTTPS or HTTP? I have used the following code
if($_SERVER['HTTPS']==on){ echo "something";}else{ echo "something other";}
But the result is not correct. Is there is any other way to identify this?
Also I have one more question.
How to get complete current url (including HTTP/HTTPS) using php?
Please help me
Thanks in advance
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") {
echo "something";
} else {
echo "something other";
}
notice the on should be a string .
You could use methods that are already defined in Zend Framework instead of explicitly using $_SERVER superglobals.
To determine if the connection is HTTP or HTTPS (this code should go into your controller):
if ( $this->getRequest()->isSecure() ) { echo 'https'; } else { echo 'http'; }
To get complete current url:
$this->getRequest()->getScheme() . '://' . $this->getRequest()->getHttpHost() . $this->getRequest()->getRequestUri();
The better way to check is
if (isset($_SERVER['HTTPS']) && $_SEREVER['HTTPS'] != 'off')
{
//connection is secure do something
}
else
{
//http is used
}
As stated in manual
Set to a non-empty value if the script
was queried through the HTTPS
protocol.
Note: Note that when using ISAPI with IIS, the value will be off if the
request was not made through the HTTPS
protocol.
you need to fix check it should be
if ($_SERVER['HTTPS'] == 'on')
or try following function
if(detect_ssl()){ echo "something";}else{ echo "something other";}
function detect_ssl() {
return ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1 || $_SERVER['SERVER_PORT'] == 443)
}
This will both check if you're using https or http and output the current url.
$https = ((!empty($_SERVER['HTTPS'])) && ($_SERVER['HTTPS'] != 'off')) ? true : false;
if($https) {
$url = "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
} else {
$url = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
}