将所有访问者的浏览器信息保存到会话中不起作用

时间:2018-10-12 11:35:52

标签: javascript php

我想在我的PHP会话中保存所有访问者的浏览器信息。

我以前在其他脚本中使用过相同的代码,但效果很好,但在此脚本中却没有。

1页:

<?php
session_start();    
?>

<script src="https://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript"></script>
<script src="src/js/a.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
    var client = new ClientJS();
    $("#browser_info").val(client.getBrowser() + " " + client.getBrowserMajorVersion());
    $("#os_info").val(client.getOS() + " " + client.getOSVersion());
    $("#screenPrint").val(client.getScreenPrint());
    $("#plugins").val(client.getPlugins());
    $("#isJava").val(client.isJava() + " " + client.getJavaVersion());
    $("#isFlash").val(client.isFlash() + " " + client.getFlashVersion());
    $("#isSilverlight").val(client.isSilverlight() + " " + client.getSilverlightVersion());
    $("#isMimeTypes").val(client.isMimeTypes() + " " + client.getMimeTypes());
    $("#fonts").val(client.getFonts());
});
</script>


<!DOCTYPE html>

并在下一页:

$_SESSION['vic_info_browser']= $_POST['browser_info'];
$_SESSION['vic_info_os']= $_POST['os_info'];
$_SESSION['vic_info_screen']= $_POST['screenPrint'];
$_SESSION['vic_info_plugins']= $_POST['plugins'];
$_SESSION['vic_info_java']= $_POST['isJava'];
$_SESSION['vic_info_flash']= $_POST['isFlash'];
$_SESSION['vic_info_silver']= $_POST['isSilverlight'];
$_SESSION['vic_info_mime']= $_POST['isMimeTypes'];
$_SESSION['vic_info_fonts']= $_POST['fonts'];

我的路径也是正确的,有人知道是什么问题吗?

  

错误是:未定义的索引:screenPrint

对于每次会话,错误不仅会出现screenPrint

1 个答案:

答案 0 :(得分:1)

我会尽力回答您的问题。在您的代码中,您没有向PHP发送任何数据。一种解决方案是使用Ajax。

<?php
session_start();    
?>

<script src="https://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript"></script>
<script src="src/js/a.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
    var client = new ClientJS();
    var browser = $("#browser_info").val(client.getBrowser() + " " + client.getBrowserMajorVersion());
    var os_info = $("#os_info").val(client.getOS() + " " + client.getOSVersion());
    var screenPrint = $("#screenPrint").val(client.getScreenPrint());
    var plugins = $("#plugins").val(client.getPlugins());
    var isJava = $("#isJava").val(client.isJava() + " " + client.getJavaVersion());
    var isFlash = $("#isFlash").val(client.isFlash() + " " + client.getFlashVersion());
    var isSilverlight = $("#isSilverlight").val(client.isSilverlight() + " " + client.getSilverlightVersion());
    var isMimeTypes = $("#isMimeTypes").val(client.isMimeTypes() + " " + client.getMimeTypes());
    var fonts = $("#fonts").val(client.getFonts());
    $.ajax({
        type      : 'POST',
        url       : 'yourphp.php',
        data      : { browser: browser, os_info: os_info, screenPrint: screenPrint, plugins: plugins, isJava: isJava, isFlash: isFlash, isSilverlight: isSilverlight, isMimeTypes: isMimeTypes, fonts: fonts  },
        success   : function(data) {

       }
    });
});
</script>


<!DOCTYPE html>

您的PHP我添加了一些验证

session_start();
if(isset($_POST['browser_info'])){
    $_SESSION['vic_info_browser']= $_POST['browser_info'];
}
if(isset($_POST['os_info'])){
    $_SESSION['vic_info_os']= $_POST['os_info'];
}
if(isset($_POST['screenPrint'])){
    $_SESSION['vic_info_screen']= $_POST['screenPrint'];
}
if(isset($_POST['plugins'])){
    $_SESSION['vic_info_plugins']= $_POST['plugins'];
}
if(isset($_POST['isJava'])){
    $_SESSION['vic_info_java']= $_POST['isJava'];
}
if(isset($_POST['isFlash'])){
    $_SESSION['vic_info_flash']= $_POST['isFlash'];
}
if(isset($_POST['isSilverlight'])){
    $_SESSION['vic_info_silver']= $_POST['isSilverlight'];
}
if(isset($_POST['isMimeTypes'])){
    $_SESSION['vic_info_mime']= $_POST['isMimeTypes'];
}
if(isset($_POST['fonts'])){
    $_SESSION['vic_info_fonts']= $_POST['fonts'];
}