PHP Get Absolute Path of Current Script

PHP Get Absolute Path of Current Script

PHP Provides quite easy ways for determining the path of current script that is being executed, stated by this take an example of the following URL:

https://(www.)domain.com/internal/script.php?var=anyvar#hashtag

USING PATHINFO()

$x = pathinfo($url);
$x['dirname']      🡺 https://example.com/internal
$x['basename']     🡺 script.php?
$x['extension']    🡺 php?var=anyvar#hashtag // Unsecure! also, read my notice about hashtag parts    
$x['filename']     🡺 script

USING PARSE_URL()

$x = parse_url($url);
$x['scheme']       🡺 https
$x['host']         🡺 example.com
$x['path']         🡺 /internal/script.php
$x['query']        🡺 var=anyvar
$x['fragment']     🡺 hashtag

 

For wordpress

//(let's say, if wordpress is installed in subdirectory:  http://example.com/wpdir/)
home_url()                      🡺 http://example.com/wpdir/        //if is_ssl() is true, then it will be "https"
get_stylesheet_directory_uri()  🡺 http://example.com/wpdir/wp-content/themes/THEME_NAME  [same: get_bloginfo('template_url') ]
get_stylesheet_directory()      🡺 /home/user/public_html/wpdir/wp-content/themes/THEME_NAME
plugin_dir_url(__FILE__)        🡺 http://example.com/wpdir/wp-content/themes/PLUGIN_NAME
plugin_dir_path(__FILE__)       🡺 /home/user/public_html/wpdir/wp-content/plugins/PLUGIN_NAME/  

 

I will write more on this soon.

0

Related Posts

Leave a Reply

avatar
  Subscribe  
Notify of