This question already has answers here:
Get the full URL in PHP
(27 answers)
Closed 8 years ago.
How to get the current full url in php?
Ex. full url: www.topclinique.ma/list-cliniques.php?t=cliniques&s=0&c=Casablanca
You can use
$current_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Try this:
if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
$protocol = 'https://';
}
else {
$protocol = 'http://';
}
$current_link = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $current_link;
Related
I am new in wordpress, Right now i am working with "my server", and my current domain/main url is like "myurl.com/myproject"
And later i will move project(wordpress) to another server and url will be like "myurl.com", so i want to know that right now how can
i manage this ? in other words which code should be use for include "css,js,images etc.." ?
I tried with following code not showing "http"
echo $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
You can get your domain URL via home_url('/') home_url and get your current directory via PHP function and constant basename(___FILE___) FILE_ basename or use WordPress function to create your desire dircetory path get_template_directory().'/your_dir' get_template_directory or URL get_template_directory_uri().'/your_dir' get_template_directory_uri
Here you go:
function get_url(){
// Start memory cache
static $parse_url;
// Return cache
if($parse_url) {
return $parse_url;
}
// Check is SSL
$is_ssl = (
( is_admin() && defined('FORCE_SSL_ADMIN') && FORCE_SSL_ADMIN ===true )
|| (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')
|| (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
|| (!empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on')
|| (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443)
|| (isset($_SERVER['HTTP_X_FORWARDED_PORT']) && $_SERVER['HTTP_X_FORWARDED_PORT'] == 443)
|| (isset($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] == 'https')
);
// Get protocol HTTP or HTTPS
$http = 'http'.( $is_ssl ? 's' : '');
// Get domain
$domain = preg_replace('%:/{3,}%i','://',rtrim($http,'/').'://'.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''));
$domain = rtrim($domain,'/');
// Combine all and get full URL
$url = preg_replace('%:/{3,}%i','://',$domain.'/'.(isset($_SERVER['REQUEST_URI']) && !empty( $_SERVER['REQUEST_URI'] ) ? ltrim($_SERVER['REQUEST_URI'], '/'): ''));
//Save to cache
$parse_url = array(
'method' => $http,
'home_fold' => str_replace($domain,'',home_url()),
'url' => $url,
'domain' => $domain,
);
// Return
return $parse_url;
}
I build this function to work in one my plugin that cover all WordPress and PHP versions.
Tested on few thousand installations.
Feel free to use it!
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;
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Check if the url is contains the http or https
What is the code to figure out whether the given URL contains http:// or https:// at the beginning using PHP and regular expressions?
you can use parse_url
<?php
$url = parse_url('https://example.org');
if ($url['scheme'] == 'https') {
// is https;
}
?>
if (substr($string, 0, 7) == "http://") {
$res = "http";
}
if (substr($string, 0, 8) == "https://") {
$res = "https";
}
Maybe this could help
$_SERVER['SERVER_PROTOCOL'];
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'];
}