php 会话变量的 str_replace

发布于 2024-11-18 11:10:27 字数 710 浏览 2 评论 0原文

我在 str_replace 行上收到错误。我试图用它的常量替换 $_SESSION 变量。我缺少什么?

$resume = fopen('creative/'.$_SESSION['user'].'/resume.php', 'r') or die('error when opening the file');
$fp = fopen('creative/'.$_SESSION['user'].'/resume2.php', 'w');
$creative = $_SESSION['creative'];
$user = $_SESSION['user'];
if ($resume){
while (($buffer = fgets($resume, 4096)) !== false) {
    str_replace("\$_SESSION[creative]", $creative, $buffer);
    str_replace("\$_SESSION[user]", $user, $buffer);
    fwrite($fp, $buffer);
    echo $buffer;
}
if (!feof($resume)) {
    echo "Error: unexpected fgets() fail\n";
}
fclose($resume);
fclose($fp);
}

我无法用常量替换 $_SESSION 变量。它不会在替换中返回 $_SESSION 变量的值。它似乎根本无法替代。

I am getting an error on the str_replace line. I am trying to replace the $_SESSION variable with the constant of it. What am i missing?

$resume = fopen('creative/'.$_SESSION['user'].'/resume.php', 'r') or die('error when opening the file');
$fp = fopen('creative/'.$_SESSION['user'].'/resume2.php', 'w');
$creative = $_SESSION['creative'];
$user = $_SESSION['user'];
if ($resume){
while (($buffer = fgets($resume, 4096)) !== false) {
    str_replace("\$_SESSION[creative]", $creative, $buffer);
    str_replace("\$_SESSION[user]", $user, $buffer);
    fwrite($fp, $buffer);
    echo $buffer;
}
if (!feof($resume)) {
    echo "Error: unexpected fgets() fail\n";
}
fclose($resume);
fclose($fp);
}

I can't get this to replace the $_SESSION variable with the constant. It is not returning the value of the $_SESSION variable in the replacement. it doesn't seem to replace at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽梦忆笙歌 2024-11-25 11:10:27

$resume 是一个资源,您不能将其用作 str_replace 的参数。您应该使用 fgets 读取文件的内容,对其进行修改,然后将其写回工作。

$resume is a resource, you can't use it as a parametr of str_replace. you should read the content of the file with fgets, modfy it and then write it back to work.

泪之魂 2024-11-25 11:10:27
str_replace('\$_SESSION['creative']', $creative, $buffer);
str_replace('\$_SESSION['user']', $user, $buffer);

双引号导致 php 尝试读取 $_SESSION 变量本身。您正在尝试替换文件中的那些作品,对吗?

str_replace('\$_SESSION['creative']', $creative, $buffer);
str_replace('\$_SESSION['user']', $user, $buffer);

The double quotes are causing php to try and read the $_SESSION variable itself. You are trying to replace those works in the file, correct?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文