dqfxao2898 2014-06-05 10:58
浏览 51
已采纳

来自URL的$ _GET只有一次

Let's say I have the following URL:

http://test/order?id=263&name=John

A php file handles the URL and I use $_GET to take the data from the URL and bind it to a variable:

<?php
    $id = $_GET['id'];
    $name = $_GET['name'];
?>

As it stands now, the user is able to change the URL and subsequently the values of the variables. I want the variables to be bound once and not to be subjected to change after. Is there any way to do that with PHP?

  • 写回答

3条回答 默认 最新

  • douzi8548 2014-06-05 11:00
    关注

    you could actually store them in session....

    1)at the very top of the page initialize the session

    2) check if the value in session exists and if not create it.

    at this point every further change will not be taken in consideration,

    <?php
    session_start();
    
    if (!isset($_SESSION['user'])) {
         $_SESSION['user'] = [
            'id' => (int) $_GET['id'], //Cast the id to int
            'name' =>urldecode($_GET['name']) //url decode the name
         ];
    }
    

    Now you have your data stored in session and you can call it using:

    $_SESSION['user']['id']

    $_SESSION['user']['name']

    and they will never be overwritten, if you want to be updated on every call or change it if some parameter has been passed you can add some option in the condition

    if (!isset($_SESSION['user']) && $_GET['updateData') == 1) {
         $_SESSION['user'] = [
            'id' => (int) $_GET['id'], //Cast the id to int
            'name' =>urldecode($_GET['name']) //url decode the name
         ];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c