I'm trying to figure out how to count the number of views that have been sent to my site from another website. I have a banner advertised and I want to have the banner link directed to a php script that will count the number of times people from website abc.com vistied the site.
The question is how do I go about doing this? I was thinking about setting up a table in mysql with a different row for each site and then have that specific row increment the count by one.
Problem is I'm not sure how to use the function i++ (if thats even right function). I am new to php, sorry if what I'm asking is a basic thing
You can get the referrer from $_SERVER['HTTP_REFERER']. It doesn't always exist so you'd need to check, and if it does then perform an upsert.
I would do something like this:
if(isset($_SERVER['HTTP_REFERER'])){
$referrer = $_SERVER['HTTP_REFERER'];
$query = "INSERT INTO 'referrals'
('referrer', 'count')
VALUES
($referrer, 1)
ON DUPLICATE KEY UPDATE
'count' = 'count' + 1";
$result = mysql_query($query);
}
Usually banners point to a specific URL on your server from the referring website so you can track where things come from.
Eg: BigSite.com has a banner for you that links to MySite.com/links/01941731.htm
Using something like .htaccess on an Apache server you can parse the incoming "01941731" part, safely check it against your database and incremement the key it relates to so that it counts against an incoming link from BigSite.com
Thats how I would do it :)
Related
Not quite sure how descriptive that title was but this is what I want to do. I scripted a URL shortener today and it's working fine, I just want to add some stats on the bottom of it saying how many links there are and how many clicks there are. Now everytime a user clicks one of the links it +1 the column in the database then it redirects the user. I want to query that and add all of those numbers together from each row. I attempted a while loop which im not surprised didn't work:
while($rows = mysql_fetch_assoc($check_count)){
$clicks = $rows['clicks'];
$clicks = $clicks+$clicks;
}
If you don't understand please ask and if you do understand it means alot for any response!
I was wondering, how is it possible to get example the last X number of pages a user came from on my site?
I am creating a navigation, so the user easily can see the X number of previuos pages he visited on my site only.
I just don't know how to do this. Is there any function in PHP to obtain this?
Thanks in advance.
I'm assuming you mean the last X pages on your site only, and that you know how to use sessions and arrays.
In this case you could initialize a session for the user, then every time the user visits a page, append the URL or some identified of that page to the array. If there's more than X pages in the array, additionally remove the first one.
Then you could just get the contents of the array, parse them into a bunch of links and show them on your site.
In case you meant the pages before entering your site, you can only get the latest one via the referer header. Some browsers might be configured to not send the referer header, however, so there really isn't a way of accomplishing this properly.
<?php
session_start();
/*
Put here any logic that could result in a redirect, to avoid useless records
...
*/
$_SESSION['history'][] = $_SERVER['REQUEST_URI'];
$_SESSION['history'] is now an indexed array with all of the user's history on your website.
I m trying to created a code for the page in which no. of views are needed to be shown in the bottom of the page.
I don't know what will be the logic behind counting the views. let's say the views like on the home page of the stackoverflow. I like they way they display. And the logic for the same. (it'll be done via the php-mysql)
Please help
Thanks in advance
Set a function to select if their IP address is already in the table where you track your hits, if it is don't add one to the count, otherwise add 1 to the count as well as their IP for future tracking.
You'd stick something like this in the header of each page:
<?php
updatePageHitCount('name of page here');
?>
where the 'name of page here' is some unique identifier for the page. The function would then basically just do some SQL:
UPDATE pagehitcounts
SET hitcount = hitcount + 1
WHERE pageID = 'name of page here';
Of course, you'd want something a little more robust, but this would be the basics.
My site has a library full of games, nations, game scenarios, etc.
library.php is given a type=___ & id=___
for example library.php?type=scenario&id=ABCD001
library.php saves the id to a session variable and loads an include appropriate for the type
This all works just dandy. Now, I wanted to give my users the option of pulling up a random scenario. To do that, I added a special id to the logic within lib-scenario.php (the include) such that if given library.php?type=scenario&id=random the include knows to run an alternate query for a random record rather than for the actual id
This also works just dandy... unless someone hits the Random Scenario button two+ times in a row, and decides that the previous random scenario was way cooler, I want to go back to that.
Because the http address is always directory/library.php?type=scenario&id=random no matter how many times you click Random Scenario, as soon as you click back you'll be taken to the last page with an alternate address you visited.
So, if you start at the Home page, and hit Random Scenario 35 times, then decide the 34th one was what you wanted and click BACK, you'll be put back onto the Home page.
I must admit this was not a problem I had anticipated. One of my testers was the first to have the urge to back-up in the random scenario stream and here we are.
How can I add back-up functionality to my script?
Make the 'Random Scenario' button simply link to an actual (but random) scenario id. You'll probably have to construct this with an SQL query to get all the id's of your scenarios.
$result = mysql_query("SELECT id FROM scenarios");
while ($row = mysql_fetch_row($result)) {
$ids[] = $row[0];
}
$randomid = array_rand($ids);
Button:
<a href="directory/library.php?type=scenario&id=<?php echo $randomid; ?>Random Scenario</a>
If your scenario id's are all consecutive numbers you can simply use this instead:
$randomid = rand($min, $max);
you can resolve this by redirecting to the canonical url for the scenario, i.e.: id=random redirects to id=A92831 or whatever was selected. the final url will be stored in the history, rather than the id=random url.
so i have a page where a user can submit his own links
i dont want to make a script that do everything automatic, i just want to make a page where, when the .php find a possible duplicated link, i get a warning like "ID 22 could possible have the same link as ID 738"
i only need like the query to do that...if its possible with that..i can only use php and mysql
if its too expensive (memory, cpu..) i can make like a submit button then when pressed generates like a report
ps: just to be clear, im not showing the message to the user, is something im going to put at my admin cp..and its not comparing "link 1" to "link 2" but searching the entire database
thanks
Your best bet would be to have a query that runs when new links are submitted. It could do something like this as a basic example:
SELECT * FROM links WHERE url = ""
If that returns results, then you have a dupe which can either be prevented from being inserted into the database, or simply flagged somewhere for review.
If you want to know which links are used by more than 1 ID, you could do something like this:
SELECT link,COUNT(id)
FROM link_table
GROUP BY link
HAVING COUNT(id) > 1