Jump to content

Getting URL of page using script embedded as img


rtadams89

Recommended Posts

I have no idea how to phrase this simply, so here is the long explanation. I wanted to track the number of people who view my ads posted on craigslist. Craigslist won't let you embed PHP, javascript, or any external files in the posts, except for images. SO here is what I did:

 

I created a file named "hit.php" on my server. In my craigslsit post, i include:

<img src="http://myserver.com/hit.php?id=1234">

 

When called, hit.php records the hit in a database and then uses a header(location) to redirect to a transparent gif file. The "id" parameter in the URL is the id of the post. This has to be hard coded into each craigslist post I make.

 

I would instead like to pull the ID from the post URL automatically. For example, in the post http://phoenix.craigslist.com/sys/1234.html the id would be "1234". Is there a way to do this when the hit.php script is being called from an "<img>" tag?

All you would do(if I read this correctly..) would be:

 

send the entire url into the id variable, then break it apart to get the "1234". For example:, exploding all the "sys/" in the variable into, say $temp and then exploding the . in the $temp[1] variable into, say, $temp2. Then $temp2[0] would have the 1234. That's how I break them up anyways. there's probably an easier way, like with preg_replace.

I'm not quite sure you understand how browsers put pages together. =P  The image tag won't send any referrer data because all the browser does it send a GET request to your server for the image (which in this case is a PHP page that sends image headers, so the browser doesn't know the difference).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.