{"id":453,"date":"2021-06-28T22:00:17","date_gmt":"2021-06-29T01:00:17","guid":{"rendered":"https:\/\/blog.galsoft.duckdns.org\/?p=453"},"modified":"2021-06-28T22:00:22","modified_gmt":"2021-06-29T01:00:22","slug":"get-full-url-en-php","status":"publish","type":"post","link":"https:\/\/blog.galsoft.com.ar\/?p=453","title":{"rendered":"get full URL en PHP"},"content":{"rendered":"<p class=\"wp-block-paragraph\">Examples for: <strong><code>https:\/\/(www.)example.com\/subFolder\/myfile.php?var=blabla#555<\/code><\/strong><\/p>\n<pre class=\"wp-block-code\"><code>\/\/ ======= PATHINFO ====== \/\/\n$x = pathinfo($url);\n$x[&#039;dirname&#039;]      \ud83e\udc7a https:\/\/example.com\/subFolder\n$x[&#039;basename&#039;]     \ud83e\udc7a                               myfile.php?var=blabla#555 \/\/ Unsecure! \n$x[&#039;extension&#039;]    \ud83e\udc7a                                      php?var=blabla#555 \/\/ Unsecure! \n$x[&#039;filename&#039;]     \ud83e\udc7a                               myfile\n\n\/\/ ======= PARSE_URL ====== \/\/\n$x = parse_url($url);\n$x[&#039;scheme&#039;]       \ud83e\udc7a https\n$x[&#039;host&#039;]         \ud83e\udc7a         example.com\n$x[&#039;path&#039;]         \ud83e\udc7a                    \/subFolder\/myfile.php\n$x[&#039;query&#039;]        \ud83e\udc7a                                          var=blabla\n$x[&#039;fragment&#039;]     \ud83e\udc7a                                                     555\n\n\/\/=================================================== \/\/\n\/\/========== self-defined SERVER variables ========== \/\/\n\/\/=================================================== \/\/\n$_SERVER[&quot;DOCUMENT_ROOT&quot;]  \ud83e\udc7a \/home\/user\/public_html\n$_SERVER[&quot;SERVER_ADDR&quot;]    \ud83e\udc7a 143.34.112.23\n$_SERVER[&quot;SERVER_PORT&quot;]    \ud83e\udc7a 80(or 443 etc..)\n$_SERVER[&quot;REQUEST_SCHEME&quot;] \ud83e\udc7a https                                         \/\/similar: $_SERVER[&quot;SERVER_PROTOCOL&quot;] \n$_SERVER[&#039;HTTP_HOST&#039;]      \ud83e\udc7a         example.com (or with WWW)             \/\/similar: $_SERVER[&quot;SERVER_NAME&quot;]\n$_SERVER[&quot;REQUEST_URI&quot;]    \ud83e\udc7a                       \/subFolder\/myfile.php?var=blabla\n$_SERVER[&quot;QUERY_STRING&quot;]   \ud83e\udc7a                                             var=blabla\n__FILE__                   \ud83e\udc7a \/home\/user\/public_html\/subFolder\/myfile.php\n__DIR__                    \ud83e\udc7a \/home\/user\/public_html\/subFolder              \/\/same: dirname(__FILE__)\n$_SERVER[&quot;REQUEST_URI&quot;]    \ud83e\udc7a                       \/subFolder\/myfile.php?var=blabla\nparse_url($_SERVER[&quot;REQUEST_URI&quot;], PHP_URL_PATH)\ud83e\udc7a  \/subFolder\/myfile.php \n$_SERVER[&quot;PHP_SELF&quot;]       \ud83e\udc7a                       \/subFolder\/myfile.php\n\n\/\/ ==================================================================\/\/\n\/\/if &quot;myfile.php&quot; is included in &quot;PARENTFILE.php&quot; , and you visit  &quot;PARENTFILE.PHP?abc&quot;:\n$_SERVER[&quot;SCRIPT_FILENAME&quot;]\ud83e\udc7a \/home\/user\/public_html\/parentfile.php\n$_SERVER[&quot;PHP_SELF&quot;]       \ud83e\udc7a                       \/parentfile.php\n$_SERVER[&quot;REQUEST_URI&quot;]    \ud83e\udc7a                       \/parentfile.php?var=blabla\n__FILE__                   \ud83e\udc7a \/home\/user\/public_html\/subFolder\/myfile.php\n\n\/\/ =================================================== \/\/\n\/\/ ================= handy variables ================= \/\/\n\/\/ =================================================== \/\/\n\/\/If site uses HTTPS:\n$HTTP_or_HTTPS = ((!empty($_SERVER[&#039;HTTPS&#039;]) &amp;&amp; $_SERVER[&#039;HTTPS&#039;]!==&#039;off&#039;) || $_SERVER[&#039;SERVER_PORT&#039;]==443) ? &#039;https:\/\/&#039;:&#039;http:\/\/&#039; );            \/\/in some cases, you need to add this condition too: if (&#039;https&#039;==$_SERVER[&#039;HTTP_X_FORWARDED_PROTO&#039;])  ...\n\n\/\/To trim values to filename, i.e. \nbasename($url)             \ud83e\udc7a myfile.php\n\n\/\/excellent solution to find origin\n$debug_files = debug_backtrace();       \n$caller_file = count($debug_files) ? $debug_files[count($debug_files) - 1][&#039;file&#039;] : __FILE__;\n<\/code><\/pre>\n<p class=\"wp-block-paragraph\"><code>DIRECTORY_SEPARATOR<\/code> returns <code>\\<\/code> for Windows-type hosting, instead of <code>\/<\/code><\/p>\n<pre class=\"wp-block-code\"><code>$uri = $_SERVER[&#039;REQUEST_SCHEME&#039;] . &#039;:\/\/&#039; . $_SERVER[&#039;HTTP_HOST&#039;].$_SERVER[&#039;REQUEST_URI&#039;]\n<\/code><\/pre>\n<p class=\"wp-block-paragraph\"><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>Para wordpress<\/strong><\/span><\/p>\n<pre class=\"wp-block-code\"><code>home_url()                      \ud83e\udc7a http:\/\/example.com\/wpdir\/        \/\/if is_ssl() is true, then it will be &quot;https&quot;\nget_stylesheet_directory_uri()  \ud83e\udc7a http:\/\/example.com\/wpdir\/wp-content\/themes\/THEME_NAME  [same: get_bloginfo(&#039;template_url&#039;) ]\nget_stylesheet_directory()      \ud83e\udc7a \/home\/user\/public_html\/wpdir\/wp-content\/themes\/THEME_NAME\nplugin_dir_url(__FILE__)        \ud83e\udc7a http:\/\/example.com\/wpdir\/wp-content\/themes\/PLUGIN_NAME\nplugin_dir_path(__FILE__)       \ud83e\udc7a \/home\/user\/public_html\/wpdir\/wp-content\/plugins\/PLUGIN_NAME\/  \n<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>Examples for: <a href=\"https:\/\/(www.)example.com\/subFolder\/myfile.php?var=blabla#555\">https:\/\/(www.)example.com\/subFolder\/myfile.php?var=blabla#555<\/a> DIRECTORY_SEPARATOR returns \\ for Windows-type hosting, instead of \/ Para wordpress<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-453","post","type-post","status-publish","format-standard","hentry","category-php","entry"],"_links":{"self":[{"href":"https:\/\/blog.galsoft.com.ar\/index.php?rest_route=\/wp\/v2\/posts\/453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.galsoft.com.ar\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.galsoft.com.ar\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.galsoft.com.ar\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.galsoft.com.ar\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=453"}],"version-history":[{"count":1,"href":"https:\/\/blog.galsoft.com.ar\/index.php?rest_route=\/wp\/v2\/posts\/453\/revisions"}],"predecessor-version":[{"id":454,"href":"https:\/\/blog.galsoft.com.ar\/index.php?rest_route=\/wp\/v2\/posts\/453\/revisions\/454"}],"wp:attachment":[{"href":"https:\/\/blog.galsoft.com.ar\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.galsoft.com.ar\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.galsoft.com.ar\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}