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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测