PHP - Find URL of script that included current document - php

I have a template I made that sets variables that rarely change, call my headers, calls my banner and sidebar, loads a variable which shows the individual pages, then calls the footer. In one of my headers, I want the URL of the page in the user's address bar. Is there a way to do this?
Currently:
<?php
$title = "MySite - Contacts";
include("header.php");
.
.
.
?>

The main variables you'll be intersted in is:
$_SERVER['REQUEST_URI'] Holds the path visited, e.g. /foo/bar
$_SERVER['PHP_SELF'] is the path to the main PHP file (NOT the file you are in as that could be an include but the actual base file)
There are a ton of other useful variables worth remembering in $_SERVER, so either just:
print_r($_SERVER);
or just visit the doc at http://php.net/manual/en/reserved.variables.server.php

the Web address of the Page being called, can be obtained from the following function ::
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;
}
I have been using this in many places, found on google.

It sounds like $_SERVER['REQUEST_URI'] is what you're after.

Related

Sending page URL with Contact Form

I am looking to dynamically submit the page URL of which a user submits a contact form from.
So for example if someone used a contact form on the home page the URL (https://www.example.com/) would be added to the email, and this would change depending on which page the user submitted the page from.
Now then, I have tried using -
$absolute_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Yes this does work, however, the URL I get through is as follows:
From URL:
http://example.com/email/index.php
This, I imagine, is because the mailer script is in its own directory and separate file, which is where $absolute_url is fetching from.
Any help is greatly appreciated.
Thanks
Cal.
You Could create a hidden input in the form that contains URL.
HTML in form
<input id="ref" name="url" type="hidden" value=""/>
Javascript
document.getElementById("ref").setAttribute("value",window.location.href);
You need current URL value. #Pupil's function can do the trick but you might have one problem.
Not every server set $_SERVER['HTTPS'] so you can get an error that says that HTTPS is not in $_SERVER array. Something like:
Notice: Undefined index: HTTPS in..
It's because you can't do compare if key doesn't exist.
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
You should use isset() because it checks whether a variable is "set" or available for reference. So you better go with:
if ( isset( $_SERVER["HTTPS"] )) {$pageURL .= "s";}
You can even suppress the possible notice using the "#" operator but you should then change function logic a bit. You could have then:
function getCurrentUrl(){
$pageURL = #( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] : 'https://'.$_SERVER["SERVER_NAME"];
$pageURL .= ( $_SERVER["SERVER_PORT"] != 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
$pageURL .= $_SERVER["REQUEST_URI"];
return $pageURL;
}
echo getCurrentUrl();
Hope this helps.
From your description, it seems, you want current page url.
Here is the function for the same:
<?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;
}
echo curPageURL();
?>

Get current page url and replace root with a domain

I would like to change the following code in the following way.
The php script gets the current url of the page of mine.
Now since I am using the descriptions of what i have on my another site i would like to put canonical tag on the page.
So I would like the script to get the url of my site and replace the www.mysite.com to www.domain2.com
How do i make it that the ,," will result the url i want? ( so i can make " rel="canonical" />
It can be beneficial for others aswell, who own more sites and have the same description and dont want to get penalized by Google Panda or have product descriptions from manufacturers.
<?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;
}
?>
I have finally found a solution to that
<?php
$Get_url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo str_replace("www.mydomain.com", "www.domain2.com", $Get_url);
?>

How to get specific part of url?

I am working on search function which I want make my search more easier and I had store href inside my db. Therefore, I need to get specific part of current page url eg : abc.php.
But now I only can get full url which is eg : http://abc_system/user/abc.php. Is it one of the solution is used substring?I am looking for some help. Hope you guys can help me out. Thanks in advanced.
This is my code which return url result:
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;
}
You need to use basename() function for get your filename from the URL string.
<?php
$url = "http://google.com/sdfsaf/abcd.php";
echo basename($url); // It will returns abcd.php
?>
Demo
Use **parse_url()** if you want to split url to its components,
For more info, Refer : http://www.php.net/manual/en/function.parse-url.php

Get the URL users came from on an Error 404 Page using PHP?

I'm currently building an Error 404 page for a website project. People are redirected to this page using .htaccess if the page they requested can't be found.
I want to display on the custom Error page the URL or link that they came from, but I can't figure out how. I had a go at this:
<?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; // Finally, Returns the page URL
}
?>
And then I added the PHP function to my Error page:
The Page You Came From Was: <?php echo curPageURL(); ?>
Sadly, all this does in the end is display the URL of my Error page, and not the one where users were just redirected from.
Anyone have any ideas?
Adam.
You should use
$_SERVER['HTTP_REFERER']
But it wont always be set.

PHP get string from a variable and avoid the reference

I'd first like to mention that I'm not looking for an alternative method. I actually want to try and figure out how to fix this. I am sure a lot of you are familiar with this code:
$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"];
}
This code gets the URL of the current page it's used on. What I want to do is get a string from the URL and put it into a session. I've tried using code such:
$_SESSION['pageURL']=$pageURL;
//and
$_SESSION['pageURL']= (string)$pageURL;
So far all of my effort have been fruitless, any help would be much appreciated...
$pageURL already is a string, so:
$_SESSION['pageURL']=$pageURL;
should work without any problems.
Are you using session_start() somewhere?
$_SESSION['pageURL']=$pageURL; //Obviously won't work
That actually works perfectly... Make sure you have called session_start()?
session_start();
$_SESSION['pageURL']=$pageURL;

Categories