dsuw85815 2015-07-19 18:18
浏览 61
已采纳

str_replace从$ _SESSION中删除字符串

I save a reuested URI into a SESSION by using:

$_SESSION['uri'] = $_SERVER["REQUEST_URI"];

Now I have a form that will be submitted and in case of no entry the page should be reloaded with the information from the GET-parameters. So I usually thought to check if the stored URI already contains the String with the GET-parameters by simply using str_replace('&error=empty', '', $_SESSION['uri']); but it seems that this function does not work and I have no clue why.

Here is an example from the original code where the form is submitted:

if (isset($_POST['send']) === true && empty($_POST['suggestion'])) {
    if ( isset($_GET['error']) === true ) {
        header ('Location: '.$_SESSION['uri'].'withoutstringerror');
    } else {    
        header ('Location: '.$_SESSION['uri'].'&error=empty');
    }

The header-function is necessary because if the form will be submitted and there is no entry all the GET-parameters will be lost. That is why I have to store the original URI and use the header-function to keep the information. The problem with the code above is that when submitting the form and there is no entry it adds the &error=emptyagain. I thought it would be the best solution to check if there is already that string in the URI and replace it by using str_replace()

So what am I doing wrong?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • donglv9813 2015-07-19 18:30
    关注
    $_SESSION['uri'] = str_replace('&error=empty', '', $_SESSION['uri']);
    

    I think you've just missed assigning the return value from str_replace() to your session variable. str_replace() doesn't modify the variable you pass in by parameter directly, it returns a new value, based on the function call.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题