base url in 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.
Also in PHP:
- create array from string with commas php
- codeigniter get lst inserted id
- check if string contains a character php
- 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))
- cart icon in woocommerce
- dateformt php
- ->with('success'
- $errors show this error in laravel
- add console script in phpcode
- encode image to base64 in php
- clear cahce config laravel in cpanel
- exception php
- < Php echo date("m") . " "; ?> what is the output of this script?
- console.log from functions php
- connect mongodb with php
- buscar texto dentro de php
- current date php function
- check key exists in associative array in php
- add 10 days to date in php
- Illuminate\Http\Request vs \Illuminate\Support\Facades\Request
- clear cache laravel and composer
- php check if string contains
- ddev laravel setup
- 2 days in php