dongyata3336 2017-08-12 09:24
浏览 22
已采纳

如何在嵌入式文件中使用php会话?

I have a site https://one.com/go.php that (for example) contains :

session_start();
$_SESSION['id_ad'] = 123456;
header('Location: https://two.com/index.php');

and on https://two.com/index.php user click and go to https://three.com/index.php that contains :

<script type="text/javascript" src="https://one.com/2.php"></script>

and finally 2.php is:

 <?php
session_start();
header("Content-Type: application/javascript");
?>
alert("<?=$_SESSION['id_ad']?>");

can i access $_SESSION['id_ad'] in 2.php ? I tested only on firefox works.

////////EDIT//////

thank you to @Dimash to mention my mistake, but the main question still exists

  • 写回答

1条回答 默认 最新

  • dstjh46606 2017-08-12 09:48
    关注

    It works, you just return javascript wrongly. Firefox for some reason works, but other browsers fails.

    Here is working code:

    <?php
    session_start();
    header("Content-Type: application/javascript");
    ?>
    alert("<?=$_SESSION['id_ad']?>");
    

    Please note, that code was tested.

    expect javascript file, not php with tag, you need return reall js file, so you need add header content type, telling that it is js file + remove script tag, as it is not html but regular js file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器