在PHP中使用msg显示消息

时间:2010-12-15 10:47:11

标签: php

我必须将a重定向到一个页面并说x已经存在。

我正在做一些这样的想法:

header('location:newcategory.php'?msg=category exists);

我更喜欢用PHP而不是JavaScript来做这件事。

3 个答案:

答案 0 :(得分:3)

在执行重定向的页面上:

header("Location: newcategory.php?msg=" . urlencode('category exists'));

在newcategory.php页面上:

echo $_GET['msg'];

答案 1 :(得分:0)

header("Location: :newcategory.php?msg=category exists&var=".$x);

在newcategory.php中添加此

if(msg == "category exists")
{
   echo $var." already exists";
}

答案 2 :(得分:0)

如果你在GET参数中传递整个消息会不会很好,因为它可能容易受到某种XSS的攻击。最好的方法是存储消息及其标识符的集合并动态生成消息。例如,你可以有一个数组:

$messages = array(
'exists' => 'category %s exists',
'failed' => 'query %s failed'
);

然后只在URL中传递消息标识符,可选的是更清楚地定义发生了什么的参数:

header("Location: newcategory.php?msg=exists&id=2");

在目标页面中,您可以编写类似的内容:

$msg = '';
switch($_GET['msg'])
    {
    case 'exists':
        {
        $msg = sprintf($messages[$_GET['msg']], intval($_GET['id']));
        break;
        }
    }
echo $msg;

它也可能包含在某些类中,但我已经向您展示了这个想法,希望它有所帮助。