Please delete me
Please delete me

Reputation:

Display a message using msg in PHP

I have to redirect a to a page and say x already exist.

I am doing some think like this:

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

I would prefer to do this from PHP, rather than in JavaScript.

Upvotes: 1

Views: 7698

Answers (3)

Tomasz Kowalczyk
Tomasz Kowalczyk

Reputation: 10467

It won't be good if you pass whole message in a GET parameter, as it would be probably vulnerable to some sort of XSS. The best way is to store collection of message and their identifiers and produce message on the fly. For example you could have an array:

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

And then pass only message identifier in an URL, optional is parameter defining more clearly what happened:

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

In target page you could write something like that:

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

It could be also wrapped in some classes, but I'd shown you the idea, hope it helps.

Upvotes: 0

praveenmon
praveenmon

Reputation: 141

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

in the newcategory.php add this

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

Upvotes: 0

Dan Grossman
Dan Grossman

Reputation: 52372

On the page doing the redirecting:

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

On the newcategory.php page:

echo $_GET['msg'];

Upvotes: 3

Related Questions