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!
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); ?>
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.
Get Last URL Segment If you want to get last URI segment, use array_pop() function 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[^>]*>(. *?)
$_SERVER['HTTP_REFERER']
will get you the referring page.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With