PHP $_POST[] 重定向后不为空

发布于 2024-11-16 21:19:53 字数 824 浏览 0 评论 0原文

我愿意接受诚实的批评和建议。

问题在于函数 $_POST[void] 在重定向后有效。 Quotes.add.php是一个表单,指向quotes.done.php,提交到mysql并使用echo $msg重定向回quotes.add.php并重置以再次填写。
是 header();在这种情况下最好的方法是什么?

报价.done.php

else{
    include 'data.php';
    $query = "INSERT INTO quotes (`id`, `quotes`, `artist`, `date`) VALUES ('null', '$_POST[text]', '$_POST[artist]', 'null')"; 
    $result = mysql_query($query) or die (mysql_error());

    $_POST['void'] = "$_POST[artist] Was Added Successfully to database";   
    unset ($_POST['artist']);       

    //var_dump($_POST['void']);
    //exit;             
    header ("location: quotes.add.php");
    exit;   
} 

报价.add.php

if (isset($_POST['void'])) {
    $msg = $_POST['void'];  
}else{
    $msg = "Please insert artist";      
}   

I'm opening myself to honest critizism and sugggestions.

The issue is with the function $_POST[void] being valid after a redirect. Quotes.add.php is a form that directs to quotes.done.php, submitted to mysql and redirected back to quotes.add.php with an echo $msg and reset to be filled out again.
Is header(); the best method in this case?

quotes.done.php

else{
    include 'data.php';
    $query = "INSERT INTO quotes (`id`, `quotes`, `artist`, `date`) VALUES ('null', '$_POST[text]', '$_POST[artist]', 'null')"; 
    $result = mysql_query($query) or die (mysql_error());

    $_POST['void'] = "$_POST[artist] Was Added Successfully to database";   
    unset ($_POST['artist']);       

    //var_dump($_POST['void']);
    //exit;             
    header ("location: quotes.add.php");
    exit;   
} 

quotes.add.php

if (isset($_POST['void'])) {
    $msg = $_POST['void'];  
}else{
    $msg = "Please insert artist";      
}   

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

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

发布评论

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

评论(4

Oo萌小芽oO 2024-11-23 21:19:53

如果您进行重定向,我认为您必须使用 $_SESSION。
我会这样做:

session_start();
$_SESSION['msg'] =  "$_POST[artist] Was Added Successfully to database";
header ("location: quotes.add.php");
exit;  

然后

session_start();
if (isset($_SESSION['msg'])) {
    $msg = $_SESSION['msg'];  
    unset($_SESSION['msg'];
}else{
    $msg = "Please insert artist";      
}    

If you do a redirect i think you have to use $_SESSION.
I'd do:

session_start();
$_SESSION['msg'] =  "$_POST[artist] Was Added Successfully to database";
header ("location: quotes.add.php");
exit;  

and then

session_start();
if (isset($_SESSION['msg'])) {
    $msg = $_SESSION['msg'];  
    unset($_SESSION['msg'];
}else{
    $msg = "Please insert artist";      
}    
幸福%小乖 2024-11-23 21:19:53

这是干净、正确且安全的方法:

quotes.done.php

<?php
else{
    include 'data.php';

    // escape the input, to avoid SQL injection
    $text   =   mysql_real_escape_string($_POST['text']);
    $artist =   mysql_real_escape_string($_POST['artist']);

    $query  = "INSERT INTO quotes (`id`, `quotes`, `artist`, `date`) VALUES ('null', '{$text}', '{$artist}', 'null')";
    $result = mysql_query($query);

    // always set your variables to a default value
    $success = false;

    // did the query execute successfully?
    if($result){
        $success = true;
    }

    header('Location: quotes.add.php?result=addedquote&artist='.urlencode($_POST['artist']).'&success='.urlencode($success));
    exit;
}
?>

quotes.add.php

<?php
$msg = '';
if(isset($_GET['result']) && $_GET['result'] == 'addedquote') {
    $artist     =   htmlentities(urldecode($_GET['artist']), ENT_QUOTES, 'UTF-8', false);
    $success    =   (bool) urldecode($_GET['success']);
    if($success){
        $msg = "$artist Was Added Successfully to database";
    } else{
        $msg = "Failed to Add $artist to database";
    }
} else{
    $msg = "Please insert artist";      
}
?>

这里需要注意一些事项:

  1. 如果您没有使用包装器来使用准备好的参数化语句来运行数据库查询,您应该至少使用mysql_real_escape_string()来删除讨厌的东西,以防止SQL注入。

  2. 正如其他人所指出的,header() 将执行 GET 请求,因此您没有收到 $_POST[void]您正在重定向的页面。这就是为什么您将在网址上使用变量将它们传输到重定向页面,然后使用 $_GET 获取它们。

  3. $_POST[somename]$_POST['somename'] 是两个不同的东西。它们会起作用,因为 PHP 会尝试查看是否存在名为 somename 的常量,如果没有,那么你很幸运,但如果有,那么所有的天都会塌下来。 p>

This is the clean, proper, and secure way to do it:

quotes.done.php

<?php
else{
    include 'data.php';

    // escape the input, to avoid SQL injection
    $text   =   mysql_real_escape_string($_POST['text']);
    $artist =   mysql_real_escape_string($_POST['artist']);

    $query  = "INSERT INTO quotes (`id`, `quotes`, `artist`, `date`) VALUES ('null', '{$text}', '{$artist}', 'null')";
    $result = mysql_query($query);

    // always set your variables to a default value
    $success = false;

    // did the query execute successfully?
    if($result){
        $success = true;
    }

    header('Location: quotes.add.php?result=addedquote&artist='.urlencode($_POST['artist']).'&success='.urlencode($success));
    exit;
}
?>

quotes.add.php

<?php
$msg = '';
if(isset($_GET['result']) && $_GET['result'] == 'addedquote') {
    $artist     =   htmlentities(urldecode($_GET['artist']), ENT_QUOTES, 'UTF-8', false);
    $success    =   (bool) urldecode($_GET['success']);
    if($success){
        $msg = "$artist Was Added Successfully to database";
    } else{
        $msg = "Failed to Add $artist to database";
    }
} else{
    $msg = "Please insert artist";      
}
?>

A couple of things for you to note here:

  1. If you are not using a wrapper for running your db queries with prepared parameterized statements, you should use at least mysql_real_escape_string() to remove the nasty stuff in order to prevent SQL injection.

  2. As noted by others header() will do a GET request, hence why you are not getting the $_POST[void] on the page you are redirecting. That's why you will use variables on your url to transfer them to the redirected page, and then fetch them with $_GET.

  3. $_POST[somename] and $_POST['somename'] are two different things. They will work, because PHP will try to see if there is a constant named somename, if there isn't one, you are lucky, but if there is one, then all sky falls down.

守护在此方 2024-11-23 21:19:53

如果您想保留 $_POST 变量,请不要重定向。重定向会导致客户端浏览器仅使用 $_GET 参数重新请求页面。

将记录保存到数据库中。获取新的记录ID,重定向到页面并传入ID。然后使用ID来获取并显示记录。

If you want to keep the $_POST variables, don't redirect. A redirect causes the client's browser to re-request the page with only $_GET params.

Save the record to the database. Get the new record ID, redirect to a page and pass in the ID. Then use the ID to fetch and display the record.

木有鱼丸 2024-11-23 21:19:53

重定向不会将值发布到页面。您可以使用会话或 GET。

$message = urlencode("$_POST[artist] Was Added Successfully to database");
header ("location: quotes.add.php?message=" . $message);

在您的quotes.add.php页面上

echo $_GET['message'];

Redirecting will not POST a value to the page. You could use session or GET.

$message = urlencode("$_POST[artist] Was Added Successfully to database");
header ("location: quotes.add.php?message=" . $message);

On your quotes.add.php page

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