是否可以将PHP代码插入到'标题("位置:example.php")'命令?

时间:2017-01-21 01:09:40

标签: php mysql sql header

我在一段php代码中有以下命令: header("Location: clivesclassiclounge_blogtest_single.php?loginsuccess");

我想修改标头来查询数据库 - 类似于: header("Location: clivesclassiclounge_blogtest_single.php?post=<?php echo $row['id']?>loginsuccess");

当我尝试设置它时,我的编码程序/文本编辑器检测到错误(也只是感觉马虎)。我觉得我正在以错误的方式解决这个问题,我真的很感激任何建议。

谢谢,

完整代码块:

function getLogin ($conn) {
    if (isset($_POST['loginSubmit'])) {
    $uid= mysqli_real_escape_string($conn, $_POST['uid']);
    $pwd= mysqli_real_escape_string($conn, $_POST['pwd']);

    $sql = "SELECT * FROM user WHERE uid='$uid' AND pwd='$pwd'";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
        if ($row = $result->fetch_assoc()) {
            $_SESSION['id'] = $row['id'];
            $_SESSION['uid'] = $row['uid'];
            header("Location: clivesclassiclounge_blogtest_single.php?loginsuccess");
            exit();
        }
    } else {
        header("Location: clivesclassiclounge_blogtest_single.php?loginfailed");
        exit();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可能希望连接URL字符串而不是回声,例如

if (mysqli_num_rows($result) > 0) {
    if ($row = $result->fetch_assoc()) {
        $_SESSION['id'] = $row['id'];
        $_SESSION['uid'] = $row['uid'];
        header("Location: clivesclassiclounge_blogtest_single.php?" . $row['id'] . "loginsuccess");
        exit();
    }
} else {
    header("Location: clivesclassiclounge_blogtest_single.php?loginfailed");
    exit();
    }
}

PHP字符串运算符 http://php.net/manual/en/language.operators.string.php

将两个字符串连接在一起 How to combine two strings together in PHP?