dongying7667 2014-07-05 09:44
浏览 28

如何防止浏览器为javascript和php加载的图像打开新会话

I have designed a system to get some user data(screen resolution and etc) by javascript. In HTML files, by javascript, screen resolutions will be send to server via get parameters of a url(an image). the problem is that when the image is requested from server, a new session instance will be created, while I expect that the session for getting the image would be the same as the session of the parent page. please, if you have any idea how could I resolve this problem. thank alot.

the codes I am using are as follows:

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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?