douxiaqin2062 2014-01-11 05:16
浏览 125
已采纳

在PHP中通过Header重定向

I am using wampserver for php and mysql database. in my php file, after executing some code to insert in mysql database, I want to redirect browser to another html page that has php code in it. This page needs value of the variable id to fetch data from my sql database. So I send value of id by below code in the end of php code.

header('Location: lostItem.php?id=$id');

The recieving file has below code to get value of id.

$id= $_GET['id'];

But, it turns out that there is no value of id passed i.e. the receiving file shows url :

http://localhost/Lost%20and%20Found/lostItem.php?id=$id 

instead of showing

http://localhost/Lost%20and%20Found/lostItem.php?id=11

I already have another page that sends same data (value of id) to receiving file. It has below code in it for that.

echo "<a class='listOfItems' href='lostItem.php?id=$id'>";
echo $item;
echo "</a>";

And that works fine. But when I try to do same thing by using header, it doesn't work. Before using header, I have made sure that variable $id has right integer value. But it doesn't send that value by using header.

Is it that data can't be sent by this method using header? If so, please suggest an alternative method.

  • 写回答

3条回答 默认 最新

  • dqj29136 2014-01-11 05:19
    关注

    basic php notation, in single quotes variables are not interpreted

    header("Location: lostItem.php?id=$id");
    

    to be strict, location should use a full URI not a relative one

    header("Location: http://www.example.com/lostItem.php?id=$id");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化