Jump to content

setting cookies from a linked file..


Dragen

Recommended Posts

Hi,

This post is partly javascript and partly php so hopefully someone can help. You can read this post: http://www.phpfreaks.com/forums/index.php/topic,160712.0.html to get an idea of what I'm trying to do.

My problem is I'm trying to use javascript to set a cookie. The script works when I directly access the file through it's url. The file is a php file, which has png headers, so the browser classes it as an image.

So if I type in it's url: mydomain.counter.png it correctly sets the cookie, then reloads the page. However if I include the image in another file, using html image tags:

<img src="http://www.mydomain.com/counter.png" />

It doesn't set the cookie, but I know it is going through the javascript as it correctly reloads the image using javascript 'location'.

Here's my javascript:

			<script language="javascript" type="text/javascript">
		<!--
		writeCookie();

		function writeCookie() 
		{
		 var date = new Date('<?php echo $this->cookie_date(); ?>');
			date.setDate(date.getDate())
		 var the_cookie_date = date.toGMTString();
		 var the_cookie = "res="+ screen.width +"x"+ screen.height +"&"+ parent.document.referrer;
		 var the_cookie = the_cookie + ";expires=" + the_cookie_date + "; path=/";
		 document.cookie = the_cookie;

		 location = '<?php echo //$_SERVER['REQUEST_URI'] . '?' . htmlspecialchars(SID); ?>';
		}
		//-->
		</script>

 

Do I need to do something special if the file is linked to from another file? I'd thought it would be  pretty similar to using:

<script language="javascript" type="text/javascript" src="http://www.mydomain.com/javascriptfile.js"></script>

 

hope i makes sense!

 

Thanks

Link to comment
Share on other sites

Not sure if this is your problem, but the browser could be caching the image, only doing the cookie stuff the first time, then using the image in the cache.

 

To test if this is the problem, try clearing your browser cache, then going to the page with the embedded PNG. It should set the cookie.

 

Fixing this is easy, just change

<img src="http://www.mydomain.com/counter.png" />

to

<img src="http://www.mydomain.com/counter.png?t=<?php echo time(); ?>" />

 

Hope this helps.

Link to comment
Share on other sites

actually, looking at it more closely it doesn't seem to be outputting the javascript at all when I link to it in an image tag.

I'm wondering if because the browser classes it as an image, and it's being called using the image tag, then perhaps javascript wont work, because y the time it's trying to use javascript I've already told the server that it's an image (which can't use javascript) and it's in an image tag...

The strange thing is, it works fine if I just access the php image directly through the url bar...

Link to comment
Share on other sites

That makes sense... I know browsers don't execute code in an image for security reasons, one option would be to use PHP to set the cookie stuff. What I mean is

 

Inside image.php:

  1) Set cookie info with PHP's setcookie() and $_COOKIE global

  2) Then open the image URL w/ the GD lib, and stream it to the browser.

In the page you want the image embedded, say

<img src='image.php' />

 

Maybe that will accomplish what you want?

 

Link to comment
Share on other sites

yeah, I'm sure that php setcookie would work, but the cookie needs to be set in javascript because I'm trying to get the screen resolution and referrer to the page, which I have to do in javascript (I know I can get the referrer in php, but don't get me started on that ;)). So I'm setting a cookie with the information, then reloading the page/image so I can then use php to read the info and enter it into a mysql database.

Link to comment
Share on other sites

How about passing the screen size to the PHP script. I mean:

<img src='' id='myImg' />
<script type='text/javascript'>
var screenWidth = <whatever>; // Figure these out with your existing JS code
var screenHeight = <whatever>;
document.getElemenById('myImg').src = "image.php?sW="+screenWidth+"&sH="+screenHeight;
</script>

Then in 'image.php':

<?php
if(is_numeric($_GET['sW'])) $sW = $_GET['sW']; else $sW = 0;
if(is_numeric($_GET['sH'])) $sH = $_GET['sH']; else $sH = 0;
$cookie = "res=".$sW."x".$sH."&".$referrer;
// setcookie(), GD image stuff, etc.
?>

That's how I'd do it. But maybe not what you wanted...

BTW, I think screen.width and screen.height don't work in IE...

 

Hope that helps.

Link to comment
Share on other sites

Thanks for all the ideas tom!

The problem is I seem to be making it as difficult for myself as possible...

 

I'm creating a hit counter for people to use and I'm wanting it to be as simple as possible, by just adding the image tag:

<img src="http://mydomain.com/counter.png" />

So I'm trying to keep all of the code inside the image file.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.