{"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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$_SERVER[&#039;REQUEST_URI&#039;]<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">$actual_link = &quot;http:\/\/$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]&quot;; <\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$actual_link = (isset($_SERVER[&#039;HTTPS&#039;]) ? &quot;https&quot; : &quot;http&quot;) . &quot;:\/\/$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]&quot;; <\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$url = &quot;http&quot; . (($_SERVER[&#039;SERVER_PORT&#039;] == 443) ? &quot;s:\/\/&quot; : &quot;:\/\/&quot;) . $_SERVER[&#039;HTTP_HOST&#039;] . $_SERVER[&#039;REQUEST_URI&#039;]; <\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$url = (isset($_SERVER[&#039;HTTPS&#039;]) ? &quot;https&quot; : &quot;http&quot;) . &quot;:\/\/$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]&quot;; <\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">function full_path() <br\/>$s = &amp;$_SERVER;<br\/>$ssl = (!empty($s[&#039;HTTPS&#039;]) &amp;&amp; $s[&#039;HTTPS&#039;] == &#039;on&#039;) ? true:false; <br\/>$sp = strtolower($s[&#039;SERVER_PROTOCOL&#039;]);<br\/>$protocol = substr($sp, 0, strpos($sp, &#039;\/&#039;)) . (($ssl) ? &#039;s&#039; : &#039;&#039;);<br\/>$port = $s[&#039;SERVER_PORT&#039;]; <br\/>$port = ((!$ssl &amp;&amp; $port==&#039;80&#039;) || ($ssl &amp;&amp; $port==&#039;443&#039;)) ? &#039;&#039; : &#039;:&#039;.$port;<br\/>$host = isset($s[&#039;HTTP_X_FORWARDED_HOST&#039;]) ? $s[&#039;HTTP_X_FORWARDED_HOST&#039;] : (isset($s[&#039;HTTP_HOST&#039;]) ? $s[&#039;HTTP_HOST&#039;] : null);<br\/>$host = isset($host) ? $host : $s[&#039;SERVER_NAME&#039;] . $port;<br\/>$uri = $protocol . &#039;:\/\/&#039; . $host . <br\/>$s[&#039;REQUEST_URI&#039;]; $segments = explode(&#039;?&#039;, $uri, 2);<br\/>$url = $segments[0];<br\/> return $url; } <\/code><\/pre> <\/div>\n<h3 id=\"solution-5\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 5:<\/label><\/span><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">$url = (isset($_SERVER[&#039;HTTPS&#039;]) &amp;&amp; $_SERVER[&#039;HTTPS&#039;] !== &#039;off&#039; ? &#039;https&#039; : &#039;http&#039;) . &#039;:\/\/&#039; . $_SERVER[&#039;HTTP_HOST&#039;] . $_SERVER[&#039;REQUEST_URI&#039;]; <\/code><\/pre> <\/div>\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, &#8230;):<\/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[&#8216;REQUEST_URI&#8217;].<\/strong><\/li>\n<\/ul>\n<p><span style=\"color: #339966;\"><strong>Function:<\/strong><\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">function getCurrentUrl($full = true) { <br\/>if (isset($_SERVER[&#039;REQUEST_URI {<br\/><br\/>$parse = parse_url( <br\/>        (isset($_SERVER[&#039;HTTPS&#039;]) &amp;&amp; strcasecmp($_SERVER[&#039;HTTPS&#039;], &#039;off&#039;) ? &#039;https:\/\/&#039; : &#039;http:\/\/&#039;) .                 \t(isset($_SERVER[&#039;HTTP_HOST&#039;]) ? $_SERVER[&#039;HTTP_HOST&#039;] : (isset($_SERVER[&#039;SERVER_NAME&#039;]) ?  \t$_SERVER[&#039;SERVER_NAME&#039;] : &#039;&#039;)) . (($full) ? $_SERVER[&#039;REQUEST_URI&#039;] : null) ); <br\/> $parse[&#039;port&#039;] = $_SERVER[&quot;SERVER_PORT&quot;]; \/\/ Setup protocol for sure (80 is default)<br\/> return http_build_url(&#039;&#039;, $parse);<br\/> } <br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><span style=\"color: #339966;\"><strong>Test code:<\/strong><\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">\/\/ Follow $_SERVER variables was set only for test $_SERVER[&#039;HTTPS&#039;] = &#039;off&#039;;<br\/> \/\/ on $_SERVER[&#039;SERVER_PORT&#039;] = &#039;9999&#039;; <br\/>\/\/ Setup $_SERVER[&#039;HTTP_HOST&#039;] = &#039;some.crazy.server.5.name:8088&#039;;<br\/> \/\/ Port is optional there <br\/>$_SERVER[&#039;REQUEST_URI&#039;] = &#039;\/150\/tail\/single\/normal?get=param&#039;; <br\/>echo getCurrentUrl(); \/\/ http:\/\/some.crazy.server.5.name:9999\/150\/tail\/single\/normal?get=param <br\/>echo getCurrentUrl(false); <br\/>\/\/ http:\/\/some.crazy.server.5.name:9999\/ <\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php function curPageURL() {<br\/> $pageURL = &#039;http&#039;; <br\/>if ($_SERVER[&quot;HTTPS&quot;] == &quot;on&quot;) {$pageURL .= &quot;s&quot;;<br\/>} $pageURL .= &quot;:\/\/&quot;;<br\/> if ($_SERVER[&quot;SERVER_PORT&quot;] != &quot;80&quot;) { <br\/>$pageURL .= $_SERVER[&quot;SERVER_NAME&quot;].&quot;:&quot;.$_SERVER[&quot;SERVER_PORT&quot;].$_SERVER[&quot;REQUEST_URI&quot;]; <br\/>} <br\/>else {<br\/> $pageURL .= $_SERVER[&quot;SERVER_NAME&quot;].$_SERVER[&quot;REQUEST_URI&quot;]; } return $pageURL;<br\/> } <br\/>?&gt; <\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">$current_url = sprint  <br\/>     ( &#039;%s:\/\/%s\/%s&#039;, <br\/>      isset($_SERVER[&#039;HTTPS&#039;]) ? &#039;https&#039; : &#039;http&#039;,<br\/>       $_SERVER[&#039;HTTP_HOST&#039;], <br\/>        $_SERVER[&#039;REQUEST_URI&#039;]<br\/> ); <\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">$base = &quot;http:\/\/$_SERVER[SERVER_NAME]:$_SERVER[SERVER_PORT]$my_web_base_path&quot;; <br\/>$url = $base . &quot;\/&quot; . dirname(dirname(__FILE__)); <\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">\/\/Fetch page URL by this<br\/> $url = $_SERVER[&#039;REQUEST_URI&#039;]; <br\/>echo &quot;$url&lt;br \/&gt;&quot;; <br\/><br\/>\/\/It will print<br\/> \/\/fetch host by this<br\/><br\/> $host=$_SERVER[&#039;HTTP_HOST&#039;];<br\/> echo &quot;$host&lt;br \/&gt;&quot;; <br\/><br\/>\/\/You can fetch the full URL by this <br\/><br\/>$fullurl = &quot;http:\/\/&quot;.$_SERVER[&#039;HTTP_HOST&#039;].$_SERVER[&#039;REQUEST_URI&#039;];<br\/> echo $fullurl; <\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php  code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">print_r($_SERVER); <\/code><\/pre> <\/div>\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: [ad type=&#8221;banner&#8221;] Note: That the double quoted string syntax is perfectly correct If we want to support both HTTP and HTTPS, we can use Using this code has\u00a0security [&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}]}}