javascriptphpsessionscreen-resolutionvisitor-statistic

Prevent new session when opening picture file with PHP


Opening a picture file with php is done in a new session, How can I prevent this behaviour?

I have put a javascript in all of my web pages to get screen resolution. Here is the javascript code:

 <script language="JavaScript">
 document.getElementById("mypictureid").src="http://www.mywebsite.com/getresolutionpic.php?w="+screen.width+"&h="+screen.height;
</script>

and the html of the pages includes:

<img id="mypictureid" src="http://mywebsite.com/otherimage.gif"

and the following php code will be executed with each page loading to store user data in $_SESSION.

$some_name = session_name("generalvisit");
session_start();
$_SESSION['entertime']=new DateTime();

and to track the user a userid is created by a mysql database and stores in $_SESSION['userid']

if(!is_numeric($_SESSION['userid']) $_SESSION['userid']=getNewUserID();

now, when the getresolutionpic.php is triggered by javascript, store screen resolution of that user stores in the $_SESSION variable of the same user. In getresolutionpic.php we have:

$some_name = session_name("generalvisit");
session_start();
if(!is_numeric($_SESSION['userid'])
{
    $_SESSION['userid']=getNewUserID();
    $_SESSION['entertime']=new DateTime();
}
$_SESSION['width']=$_GET['w'];
$_SESSION['height']=$_GET['h'];

The problem is that when getresolutionpic.php is called, $_SESSION['userid'] is empty and a new userid will be created while the user is not actually new.

Do you have any idea where it goes wrong?


Solution

  • I think my mistake that caused this problem was that some where I had used http://mywebsite.com/etc.. instead of http://www.mywebsite.com/etc.. so the $_SESSION variable created in the former was unavailable in the later pages.

    But I solved the problem by some changes in the way codes executes. Now, I read the w and h inputs in all the pages in the domain, not just in getresolutionpic.php script. and I treat getresolutionpic.php just like any other php file in the site. in fact, getresolutionpic.php will do nothing more than reading image file and sending it to the user.

    hope I have been clear enough to be helpful for people who face the same problem in the future.