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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)