Get $_SERVER["REQUEST_URI"] in PHP5 running on Windows - php

It does not seem as simple. I use this popular function to get page full URL:
private function getBaseUrl() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
My PHP5 is on Windows server! That is why $_SERVER["REQUEST_URI"] is empty. I used this workaround for Windows:
function fixRequestURI() {
if(!isset($_SERVER['REQUEST_URI'])) {
$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
if($_SERVER['QUERY_STRING']) {
$_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
}
}
}
However, it kinda works. My full URL is:
http://www.domain.com/web/en/contact/
Strangely, I get this as a result of my functions above:
http://www.domain.com/directory1/web/en/contact/index.php?go=3
This, so called, "directory1" is my physical directory to which domain.com is mapped. I understand the implications but... how would I get my full URL easily without any fixes, adjustments, etc. just like in JavaScript using document.location.href?
Thanks

Try this:
<?
function getUrl(){
$http = 'http' . (#$_SERVER['HTTPS'] ? 's' : '');
$query = $_SERVER['QUERY_STRING'] ? "?$_SERVER[QUERY_STRING]" : '';
return "$http://$_SERVER[HTTP_HOST]$_SERVER[PHP_SELF]$query";
}
It won't catch the anchors though (after the hash # tag)

Answering my question: this worked for me:
function getFullUrl() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER['HTTP_X_REWRITE_URL'];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER['HTTP_X_REWRITE_URL'];//$_SERVER["REQUEST_URI"] blank
}
return $pageURL;
}
Please send me any thoughts if it can ever fail in any special circumstances.

Related

WordPress Get Permalink Doesn't Get URL 'As Is'

I'm using the function get_permalink() to try and get the Current URL.
I've noticed it doesn't actually get the exact URL as shown in the address bar, for example if it is formatted like:
domain.com?s=one&two&three=0&four=
Is there a function to get the exact current URL?
<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":". $_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"]. $_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>

Get full page URL using php with port number and https

I was looking for the php function that will return the full page url of the page (even rewritten with htaccess).
function returning for example:
https://google.com:8000/yourfolder/yourpage.html
<?php
function fullpageurl() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
Explanation
using $_SERVER["HTTPS"] to check if the SSL is ON or OFF
using $_SERVER["SERVER_PORT"] to check the port number of the website accessed
using $_SERVER["SERVER_NAME"] to get the website host.
using $_SERVER["REQUEST_URI"] to get the current page.

Is the current page the base url in PHP?

I need to see if the current page a user is on is the main page of the website, i.e. there is nothing after the base url.
I'm doing this to exclude some code off the main page.
I asked this question is Javascript, but would like to implement it in PHP
This will probably give you what you are looking for:
$is_home = $_SERVER[ 'REQUEST_URI' ] === '/' ? true : false;
<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
This should give you the url of the current page, when you have this you can check it against the home url.
The following should do the trick:
if (empty($_SERVER['QUERY_STRING'])) {
// no params
}

php get_headers is very slow (takes infinite!) when it requests the URI of current page

Why it takes an infinite amount of time for this ?
function getCurrentPageUrl() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
$headers = get_headers(getCurrentPageUrl());
It's a recursive request. The problem is that you are requesting to load this script and the script which is loading is trying to load it in its self an so on and so on :)

PHP URL Redirect (Getting full URL + GET)

How would I get just the last part to my URL?
Ex.
?page=home&optional=lol
I am not familiar with any of the server commands, so much help would be appreciated. Also note that the GET variables are dynamic and will be different.
The QUERY_STRING server variable should be what you're looking for:
$_SERVER['QUERY_STRING'];
Use parse_url
Getting the full URL of the page is a bit complicated, but not very:
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
$parsed_url = parse_url($pageURL);
$qs = $parsed_url['query']; //query string, this is the ? part of the URL
This can be found in $_SERVER['REQUEST_URI']

Categories