Examples for: https://www.site.xyz/fold/my.php?var=bla#555
Notice 1:
- the hashtag
#
part was added manually in below php example snippet only for theoretical & illustrational purposes, however Server-Side languages (including PHP) are unable to detect them, because hashtags parts are only recognized in Client-Side browser/javascript...*
Notice 2:
DIRECTORY_SEPARATOR
returns \
for Windows-OS, while UNIX returns /
.
========== PATHINFO (use for filepathes, not urls) ==========
$x = PATHINFO ($url);
$x['dirname'] 🡺 https://www.site.xyz/fold
$x['basename'] 🡺 my.php?var=bla#555 // unsafe!
$x['extension'] 🡺 php?var=bla#555 // unsafe!
$x['filename'] 🡺 my
========== PARSE_URL (use for urls, not filepathes) ==========
$x = PARSE_URL ($url);
$x['scheme'] 🡺 https
$x['host'] 🡺 www.site.xyz
$x['path'] 🡺 /fold/my.php
$x['query'] 🡺 var=bla
$x['fragment'] 🡺 555
Global variable $_SERVER
$_SERVER["DOCUMENT_ROOT"] 🡺 /home/user/public_html
$_SERVER["SERVER_ADDR"] 🡺 143.34.112.23
$_SERVER["SERVER_PORT"] 🡺 80 (or 443, etc..)
$_SERVER["REQUEST_SCHEME"] 🡺 https
$_SERVER["SERVER_PROTOCOL"]🡺 <same>
$_SERVER['HTTP_HOST'] 🡺 www.site.xyz
$_SERVER["SERVER_NAME"] 🡺 <same>
$_SERVER["REQUEST_URI"] 🡺 /fold/my.php?var=bla
$_SERVER["QUERY_STRING"] 🡺 var=bla
__FILE__ 🡺 /home/user/public_html/fold/my.php
$_SERVER["SCRIPT_FILENAME"]🡺 <same> // Note! it will show entry file's path, if `my.php` is not called directly.
__DIR__ 🡺 /home/user/public_html/fold
dirname(__FILE__) 🡺 <same>
$_SERVER["REQUEST_URI"] 🡺 /fold/my.php?var=bla // Note! it will show entry file's path, if `my.php` is not called directly.
parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH)🡺 /fold/my.php
$_SERVER["PHP_SELF"] 🡺 /fold/my.php // Note! it will show entry file's path, if `my.php` is not called directly.
// detect https
$is_HTTPS = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off') || $_SERVER['SERVER_PORT']==443); //in some cases, you need to add this condition too: if ('https'==$_SERVER['HTTP_X_FORWARDED_PROTO'])
PHP Functions
dirname($url) 🡺 https://www.site.xyz/fold/
basename($url) 🡺 my.php
debug_backtrace() 🡺 SHOWS FULL TRACE OF ALL INCLUDED FILES
< For WordPress >
// (Let's say, if WordPress is installed in subdirectory: http://site.xyz/sub/)
home_url() 🡺 http://site.xyz/sub/ // If is_ssl() is true, then it will be "https"
get_stylesheet_directory_uri() 🡺 http://site.xyz/sub/wp-content/themes/THEME_NAME [same: get_bloginfo('template_url') ]
get_stylesheet_directory() 🡺 /home/user/public_html/sub/wp-content/themes/THEME_NAME
plugin_dir_url(__FILE__) 🡺 http://site.xyz/sub/wp-content/themes/PLUGIN_NAME
plugin_dir_path(__FILE__) 🡺 /home/user/public_html/sub/wp-content/plugins/PLUGIN_NAME/