{"id":841,"date":"2017-03-18T18:36:38","date_gmt":"2017-03-18T13:06:38","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=841"},"modified":"2018-10-26T18:52:25","modified_gmt":"2018-10-26T13:22:25","slug":"how-to-get-full-url-in-php","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/how-to-get-full-url-in-php\/","title":{"rendered":"How to get the full URL in PHP"},"content":{"rendered":"<h2 id=\"to-get-full-url-in-php\"><span style=\"color: #800080;\">To Get Full URL\u00a0 in PHP:<\/span><\/h2>\n<p>In this post, We are discussing about How to get the full URL in <a href=\"https:\/\/www.wikitechy.com\/step-by-step-tutorials\/php\/php-introduction\" target=\"_blank\" rel=\"noopener\">PHP<\/a>.<\/p>\n<h3 id=\"solution-1\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 1:<\/label><\/span><\/h3>\n<p>Have a look at:<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24_SERVER%5B\u2019REQUEST_URI\u2019%5D%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n[pastacode lang=\u201djavascript\u201d manual=\u201d%24actual_link%20%3D%20%22http%3A%2F%2F%24_SERVER%5BHTTP_HOST%5D%24_SERVER%5BREQUEST_URI%5D%22%3B%20%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><span style=\"color: #800000;\"><strong>Note<\/strong>:<\/span> That the <strong>double quoted string<\/strong> syntax is perfectly correct<\/p>\n<ul>\n<li>If we want to support both <strong>HTTP and HTTPS<\/strong>, we can use<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24actual_link%20%3D%20(isset(%24_SERVER%5B\u2019HTTPS\u2019%5D)%20%3F%20%22https%22%20%3A%20%22http%22)%20.%20%22%3A%2F%2F%24_SERVER%5BHTTP_HOST%5D%24_SERVER%5BREQUEST_URI%5D%22%3B%20%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>Using this code has\u00a0<b>security implications<\/b>. The client can set <strong>HTTP_HOST and REQUEST_URI<\/strong> to any arbitrary value<\/li>\n<\/ul>\n<h3 id=\"solution-2\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 2:<\/label><\/span><\/h3>\n<ul>\n<li>Using a\u00a0<strong>ternary<\/strong> statement:<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24url%20%3D%20%22http%22%20.%20((%24_SERVER%5B\u2019SERVER_PORT\u2019%5D%20%3D%3D%20443)%20%3F%20%22s%3A%2F%2F%22%20%3A%20%22%3A%2F%2F%22)%20.%20%24_SERVER%5B\u2019HTTP_HOST\u2019%5D%20.%20%24_SERVER%5B\u2019REQUEST_URI\u2019%5D%3B%20%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<h3 id=\"solution-3\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 3:<\/label><\/span><\/h3>\n<ul>\n<li><strong>Cross platform<\/strong> method for finding the current URL is:<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24url%20%3D%20(isset(%24_SERVER%5B\u2019HTTPS\u2019%5D)%20%3F%20%22https%22%20%3A%20%22http%22)%20.%20%22%3A%2F%2F%24_SERVER%5BHTTP_HOST%5D%24_SERVER%5BREQUEST_URI%5D%22%3B%20%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"solution-4\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 4:<\/label><\/span><\/h3>\n<p><span style=\"color: #339966;\"><b>Sample code:<\/b><\/span><\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201dfunction%20full_path()%20%0A%24s%20%3D%20%26%24_SERVER%3B%0A%24ssl%20%3D%20(!empty(%24s%5B\u2019HTTPS\u2019%5D)%20%26%26%20%24s%5B\u2019HTTPS\u2019%5D%20%3D%3D%20\u2019on\u2019)%20%3F%20true%3Afalse%3B%20%0A%24sp%20%3D%20strtolower(%24s%5B\u2019SERVER_PROTOCOL\u2019%5D)%3B%0A%24protocol%20%3D%20substr(%24sp%2C%200%2C%20strpos(%24sp%2C%20\u2019%2F\u2019))%20.%20((%24ssl)%20%3F%20\u2019s\u2019%20%3A%20\u201d)%3B%0A%24port%20%3D%20%24s%5B\u2019SERVER_PORT\u2019%5D%3B%20%0A%24port%20%3D%20((!%24ssl%20%26%26%20%24port%3D%3D\u201980\u2019)%20%7C%7C%20(%24ssl%20%26%26%20%24port%3D%3D\u2019443\u2032))%20%3F%20\u201d%20%3A%20\u2019%3A\u2019.%24port%3B%0A%24host%20%3D%20isset(%24s%5B\u2019HTTP_X_FORWARDED_HOST\u2019%5D)%20%3F%20%24s%5B\u2019HTTP_X_FORWARDED_HOST\u2019%5D%20%3A%20(isset(%24s%5B\u2019HTTP_HOST\u2019%5D)%20%3F%20%24s%5B\u2019HTTP_HOST\u2019%5D%20%3A%20null)%3B%0A%24host%20%3D%20isset(%24host)%20%3F%20%24host%20%3A%20%24s%5B\u2019SERVER_NAME\u2019%5D%20.%20%24port%3B%0A%24uri%20%3D%20%24protocol%20.%20\u2019%3A%2F%2F\u2019%20.%20%24host%20.%20%0A%24s%5B\u2019REQUEST_URI\u2019%5D%3B%20%24segments%20%3D%20explode(\u2018%3F\u2019%2C%20%24uri%2C%202)%3B%0A%24url%20%3D%20%24segments%5B0%5D%3B%0A%20return%20%24url%3B%20%7D%20%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"solution-5\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 5:<\/label><\/span><\/h3>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%24url%20%3D%20(isset(%24_SERVER%5B\u2019HTTPS\u2019%5D)%20%26%26%20%24_SERVER%5B\u2019HTTPS\u2019%5D%20!%3D%3D%20\u2019off\u2019%20%3F%20\u2019https\u2019%20%3A%20\u2019http\u2019)%20.%20\u2019%3A%2F%2F\u2019%20.%20%24_SERVER%5B\u2019HTTP_HOST\u2019%5D%20.%20%24_SERVER%5B\u2019REQUEST_URI\u2019%5D%3B%20%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>It works for all <strong>webservers<\/strong> (<a href=\"https:\/\/www.wikitechy.com\/tutorials\/apache\/what-is-apache\" target=\"_blank\" rel=\"noopener\">Apache<\/a>, nginx, IIS, \u2026):<\/li>\n<\/ul>\n<h3 id=\"solution-6\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 6:<\/label><\/span><\/h3>\n<ul>\n<li>we can get full current URL including <strong>$_SERVER[\u2018REQUEST_URI\u2019].<\/strong><\/li>\n<\/ul>\n<p><span style=\"color: #339966;\"><strong>Function:<\/strong><\/span><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201dfunction%20getCurrentUrl(%24full%20%3D%20true)%20%7B%20%0Aif%20(isset(%24_SERVER%5B\u2019REQUEST_URI%20%7B%0A%0A%24parse%20%3D%20parse_url(%20%0A%20%20%20%20%20%20%20%20(isset(%24_SERVER%5B\u2019HTTPS\u2019%5D)%20%26%26%20strcasecmp(%24_SERVER%5B\u2019HTTPS\u2019%5D%2C%20\u2019off\u2019)%20%3F%20\u2019https%3A%2F%2F\u2019%20%3A%20\u2019http%3A%2F%2F\u2019)%20.%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%09(isset(%24_SERVER%5B\u2019HTTP_HOST\u2019%5D)%20%3F%20%24_SERVER%5B\u2019HTTP_HOST\u2019%5D%20%3A%20(isset(%24_SERVER%5B\u2019SERVER_NAME\u2019%5D)%20%3F%20%20%09%24_SERVER%5B\u2019SERVER_NAME\u2019%5D%20%3A%20\u201d))%20.%20((%24full)%20%3F%20%24_SERVER%5B\u2019REQUEST_URI\u2019%5D%20%3A%20null)%20)%3B%20%0A%20%24parse%5B\u2019port\u2019%5D%20%3D%20%24_SERVER%5B%22SERVER_PORT%22%5D%3B%20%2F%2F%20Setup%20protocol%20for%20sure%20(80%20is%20default)%0A%20return%20http_build_url(\u201d%2C%20%24parse)%3B%0A%20%7D%20%0A%7D%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><span style=\"color: #339966;\"><strong>Test code:<\/strong><\/span><\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%2F%2F%20Follow%20%24_SERVER%20variables%20was%20set%20only%20for%20test%20%24_SERVER%5B\u2019HTTPS\u2019%5D%20%3D%20\u2019off\u2019%3B%0A%20%2F%2F%20on%20%24_SERVER%5B\u2019SERVER_PORT\u2019%5D%20%3D%20\u20199999\u2019%3B%20%0A%2F%2F%20Setup%20%24_SERVER%5B\u2019HTTP_HOST\u2019%5D%20%3D%20\u2019some.crazy.server.5.name%3A8088\u2019%3B%0A%20%2F%2F%20Port%20is%20optional%20there%20%0A%24_SERVER%5B\u2019REQUEST_URI\u2019%5D%20%3D%20\u2019%2F150%2Ftail%2Fsingle%2Fnormal%3Fget%3Dparam\u2019%3B%20%0Aecho%20getCurrentUrl()%3B%20%2F%2F%20http%3A%2F%2Fsome.crazy.server.5.name%3A9999%2F150%2Ftail%2Fsingle%2Fnormal%3Fget%3Dparam%20%0Aecho%20getCurrentUrl(false)%3B%20%0A%2F%2F%20http%3A%2F%2Fsome.crazy.server.5.name%3A9999%2F%20%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"solution-7\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 7:<\/label><\/span><\/h3>\n<ul>\n<li><span style=\"color: #339966;\">\u00a0<strong>Function to handle the URL:<\/strong><\/span><\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%20function%20curPageURL()%20%7B%0A%20%24pageURL%20%3D%20\u2019http\u2019%3B%20%0Aif%20(%24_SERVER%5B%22HTTPS%22%5D%20%3D%3D%20%22on%22)%20%7B%24pageURL%20.%3D%20%22s%22%3B%0A%7D%20%24pageURL%20.%3D%20%22%3A%2F%2F%22%3B%0A%20if%20(%24_SERVER%5B%22SERVER_PORT%22%5D%20!%3D%20%2280%22)%20%7B%20%0A%24pageURL%20.%3D%20%24_SERVER%5B%22SERVER_NAME%22%5D.%22%3A%22.%24_SERVER%5B%22SERVER_PORT%22%5D.%24_SERVER%5B%22REQUEST_URI%22%5D%3B%20%0A%7D%20%0Aelse%20%7B%0A%20%24pageURL%20.%3D%20%24_SERVER%5B%22SERVER_NAME%22%5D.%24_SERVER%5B%22REQUEST_URI%22%5D%3B%20%7D%20return%20%24pageURL%3B%0A%20%7D%20%0A%3F%3E%20%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"solution-8\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 8:<\/label><\/span><\/h3>\n<ul>\n<li><strong><span style=\"color: #339966;\">HTTPS support:<\/span><\/strong><\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%24current_url%20%3D%20sprint%20%20%0A%20%20%20%20%20(%20\u2019%25s%3A%2F%2F%25s%2F%25s\u2019%2C%20%0A%20%20%20%20%20%20isset(%24_SERVER%5B\u2019HTTPS\u2019%5D)%20%3F%20\u2019https\u2019%20%3A%20\u2019http\u2019%2C%0A%20%20%20%20%20%20%20%24_SERVER%5B\u2019HTTP_HOST\u2019%5D%2C%20%0A%20%20%20%20%20%20%20%20%24_SERVER%5B\u2019REQUEST_URI\u2019%5D%0A%20)%3B%20%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"solution-9\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 9:<\/label><\/span><\/h3>\n<p><span style=\"color: #339966;\"><strong>Code:<\/strong><\/span><\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%24base%20%3D%20%22http%3A%2F%2F%24_SERVER%5BSERVER_NAME%5D%3A%24_SERVER%5BSERVER_PORT%5D%24my_web_base_path%22%3B%20%0A%24url%20%3D%20%24base%20.%20%22%2F%22%20.%20dirname(dirname(__FILE__))%3B%20%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"solution-10\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 10:<\/label><\/span><\/h3>\n<ul>\n<li><strong><span style=\"color: #339966;\">Here is the another way of solution:<\/span><\/strong><\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%2F%2FFetch%20page%20URL%20by%20this%0A%20%24url%20%3D%20%24_SERVER%5B\u2019REQUEST_URI\u2019%5D%3B%20%0Aecho%20%22%24url%3Cbr%20%2F%3E%22%3B%20%0A%0A%2F%2FIt%20will%20print%0A%20%2F%2Ffetch%20host%20by%20this%0A%0A%20%24host%3D%24_SERVER%5B\u2019HTTP_HOST\u2019%5D%3B%0A%20echo%20%22%24host%3Cbr%20%2F%3E%22%3B%20%0A%0A%2F%2FYou%20can%20fetch%20the%20full%20URL%20by%20this%20%0A%0A%24fullurl%20%3D%20%22http%3A%2F%2F%22.%24_SERVER%5B\u2019HTTP_HOST\u2019%5D.%24_SERVER%5B\u2019REQUEST_URI\u2019%5D%3B%0A%20echo%20%24fullurl%3B%20%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<h3 id=\"solution-11\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 11:<\/label><\/span><\/h3>\n<ul>\n<li><span style=\"color: #339966;\"><strong>You can use this code:<\/strong><\/span><\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201dprint_r(%24_SERVER)%3B%20%0A\u201d message=\u201dphp  code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li><b>$_SERVER <\/b>is an array containing information such as headers, paths, and script locations.<\/li>\n<li>The entries in this array are created by the <a href=\"https:\/\/www.wikitechy.com\/final-year-project\/dotnet\/server-hacking\/what-is-web-server\" target=\"_blank\" rel=\"noopener\">web server<\/a>.<\/li>\n<li><b>$HTTP_SERVER_VARS <\/b>contains the same initial information, but is not a superglobal. \u00a0 (Note that\u00a0<b>$HTTP_SERVER_VARS <\/b>and <b>$_SERVER <\/b>are different variables.)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>To Get Full URL\u00a0 in PHP: In this post, We are discussing about How to get the full URL in PHP. SOLUTION 1: Have a look at: [pastacode lang=\u201dphp\u201d manual=\u201d%24_SERVER%5B\u2019REQUEST_URI\u2019%5D%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] [ad type=\u201dbanner\u201d] [pastacode lang=\u201djavascript\u201d manual=\u201d%24actual_link%20%3D%20%22http%3A%2F%2F%24_SERVER%5BHTTP_HOST%5D%24_SERVER%5BREQUEST_URI%5D%22%3B%20%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] Note: That the double quoted string syntax is perfectly correct If we [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[1623,1621,1630,1628,1627,1620,1618,1619,1626,1622,1625,1624,1629,1634,1632,1631,1633,1617],"class_list":["post-841","post","type-post","status-publish","format-standard","hentry","category-php","tag-get-a-url-parameter-with-php","tag-get-current-page-full-url-in-php","tag-get-current-page-url-javascript","tag-get-current-url-in-wordpress","tag-get-full-url-from-address-bar-in-php-or-javascript-php-request_uri","tag-get-full-website-url","tag-how-do-i-get-the-visible-url-not-that-actual-url-in-php-after-using-htaccess-rewrites","tag-how-to-capture-a-website-url-in-the-shopify-contact-form","tag-how-to-get-current-page-url-in","tag-how-to-get-the-site-name-from-url-in-php","tag-how-to-get-url-of-current-page-in-php","tag-php-what-_server-variable-provide-full","tag-php-get-current-url-path","tag-php-get-current-url-without-query-string","tag-php-get-full-url-with-parameters","tag-php-get-url-parameter","tag-php-get-url-path","tag-reading-url-in-php"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=841"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/841\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}