How do I get the base URL with PHP?
Try this:
<?php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>
Learn more about the $_SERVER
predefined variable.
If you plan on using https, you can use this:
function url(){
return sprintf(
"%s://%s%s",
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
$_SERVER['SERVER_NAME'],
$_SERVER['REQUEST_URI']
);
}
echo url();
#=> http://127.0.0.1/foo
Per this answer, please make sure to configure your Apache properly so you can safely depend on SERVER_NAME
.
<VirtualHost *>
ServerName example.com
UseCanonicalName on
</VirtualHost>
NOTE: If you're depending on the HTTP_HOST
key (which contains user input), you still have to make some cleanup, remove spaces, commas, carriage return, etc. Anything that is not a valid character for a domain. Check the PHP builtin parse_url function for an example.
Get base url from string
Use parse_url()
$url = "http://example.com/index.php?data=data&value=value";
$url_info = parse_url($url);
echo $url_info['host'];//hostname
Additionally $user_info contain below
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
SO you can get http by scheme
you can do something like :
echo $url_info['scheme'] . '://' . $url_info['host'];
//http://example.com
How to get only base url php
Use parse_url from php.net
parse_url
<?php
$url = "https://www.stackoverflow.com/";
echo $domain = parse_url($url, PHP_URL_HOST);
?>
Output: www.stackoverflow.com
<?php
$url = '//www.example.com/path?googleguy=googley';
// Prior to 5.4.7 this would show the path as "//www.example.com/path"
echo "<pre>";
var_dump(parse_url($url));
echo "</pre>";
?>
<?php
echo "<pre>";
var_dump(parse_url($url));
var_dump(parse_url($url, PHP_URL_SCHEME));
var_dump(parse_url($url, PHP_URL_USER));
var_dump(parse_url($url, PHP_URL_PASS));
var_dump(parse_url($url, PHP_URL_HOST));
var_dump(parse_url($url, PHP_URL_PORT));
var_dump(parse_url($url, PHP_URL_PATH));
var_dump(parse_url($url, PHP_URL_QUERY));
var_dump(parse_url($url, PHP_URL_FRAGMENT));
echo "</pre>";
?>
With echo "<pre>"; echo "</pre>";
you can style the output from var_dump, looks better
PHP how to get the base domain/url?
Use SERVER_NAME
.
echo $_SERVER['SERVER_NAME']; //Outputs www.example.com
<?php echo base_url();?> and <?php base_url();?>
Please show more about your problem detail like paste your error code in here that we could use those information to help you.
Btw, base_url() isn't a php embedded function, one of the potential problem is that your friend custom a function named base_url() that supposed to return it's root path.
So, you have to either include or build the same function with the same name to your script.
Ex.
function base_url()
{
return dirname($_SERVER['SCRIPT_FILENAME']);
}
And the 'src' inside your tag should point to your jQuery source file.
Get base url in blade laravel 5.7
<meta property="og:image" content="{{ url('/path_to_image') }}">
Ref. : https://laravel.com/docs/5.7/helpers
how to get the base url in javascript
Base URL in JavaScript
You can access the current url quite easily in JavaScript with window.location
You have access to the segments of that URL via this locations
object. For example:
// This article:
// https://stackoverflow.com/questions/21246818/how-to-get-the-base-url-in-javascript
var base_url = window.location.origin;
// "http://stackoverflow.com"
var host = window.location.host;
// stackoverflow.com
var pathArray = window.location.pathname.split( '/' );
// ["", "questions", "21246818", "how-to-get-the-base-url-in-javascript"]
In Chrome Dev Tools, you can simply enter window.location
in your console and it will return all of the available properties.
Further reading is available on this Stack Overflow thread
Related Topics
How to Remove All Non Printable Characters in a String
Get Specific Columns Using "With()" Function in Laravel Eloquent
What's the Difference Between Isset() and Array_Key_Exists()
Replacing Accented Characters PHP
"Connect Failed: Access Denied For User 'Root'@'Localhost' (Using Password: Yes)" from PHP Function
How to Solve "Fatal Error: Class 'MySQLi' Not Found"
Why Is the Hash Part of the Url Not Available on the Server Side
How to Get Count of Rows in MySQL Table Using PHP
PHP - How to Create a Newline Character
Are PHP Variables Passed by Value or by Reference
How to Extend a Class Using More Than 1 Class in PHP
How to Catch Curl Errors in PHP
Preserve Line Breaks from Textarea
Special Characters in Property Name of Object
How to Perform Static Code Analysis in PHP
What's the Difference Between :: (Double Colon) and -≫ (Arrow) in PHP