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 Opencv配置出错
  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?