Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get URL of calling webpage in PHP

Tags:

url

php

I am working on a website statistics engine for a class. The idea being that you can simply embed a little code in your web page which will call the stats website on every page load and the stats website will then track your hits and such… nothing ground breaking.

What I would like to do is be able to break down website hits by webpage. For instance, a person can include the same code on each page, and the stats website will know which page got hit how many times. Is there a way in PHP to obtain the URL of the calling page (the page on which the embedded code exists)? I know how to get the URL of the page in which the PHP code is running, but not the calling page.

Alternatively, I could probably use some JavaScript to pass the page URL to the stats website, but the less code that needs to be embedded the better so I’m hoping for a PHP solution.

Thanks in advance for any and all help!

like image 208
Nate Avatar asked Aug 11 '10 17:08

Nate


People also ask

How to call the URL in PHP?

php // create a new cURL resource $ch = curl_init(); // set URL and other appropriate options curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); curl_setopt($ch, CURLOPT_HEADER, 0); // grab URL and pass it to the browser curl_exec($ch); // close cURL resource, and free up system resources curl_close($ch); ?>

What is base URL in PHP?

Base URL is used to create internal web page links dynamically in the website. You can get the base URL from the full URL string using PHP. The parse_url() function helps to parse components from URL in PHP. The base URL can be retrieved from a string using PHP parse_url() function.24-Feb-2022.

How can I get the last part of a URL in PHP?

Get Last URL Segment If you want to get last URI segment, use array_pop() function in PHP.

How can I get current page title in PHP?

PHP Code to Get Webpage Title from URL: php // function to get webpage title function getTitle($url) { $page = file_get_contents($url); $title = preg_match('/<title[^>]*>(. *?)


2 Answers

$_SERVER['HTTP_REFERER'] will get you the referring page.

like image 83
Ian Wetherbee Avatar answered Sep 22 '22 19:09

Ian Wetherbee


In addition to $_SERVER['HTTP_REFERER'] since there may be large chunks of the URI you wish to ignore, you can just pass the page name as a request param to the called script.

I.E. on the page with stats tracking you request

URL/stats_tracker.php?page=checkout

on the stats tracker page you just look for

$_REQUEST['page'];

This can be cleaner than looking at the whole URL.

like image 21
Zak Avatar answered Sep 21 '22 19:09

Zak