6

I am trying to pass a variable within a header link. I have tried below code

$name = "mike";

if($name != "lopan"){
        header("Location:error-page.php?$errorMssg=Please enter information?");
}

This page redirect to location but doesn't pass the variable that contains the message. But when i create a simple link with values like this:

<a href="error-page.php?$errorMssg=so what happened there buddy?">link</a>

it passes it just fine.

Any ideas what i am doing wrong? or i can not pass information with headers?

1
  • 1
    You don't need the $ character in the URL Commented Apr 6, 2012 at 20:12

3 Answers 3

13

You need to use urlencode like this:

if($name != "lopan"){
        header("Location:error-page.php?errorMssg=".urlencode("Waoo so your not EVEN going to try to enter information huh?"));
}

And in error-page.php, you should get it (no need to urldecode):

<?php
$errorMssg = $_GET['errorMssg'];
Sign up to request clarification or add additional context in comments.

2 Comments

Lols, is this a header thing as far as not adding the "$" in the link or??? because when i did a regular link with it, it passed just fine.
You don't need any $'s when passing arguments in urls. You need a ? to indicate the beginning of the parameters, and a & between each, like this: http://example.com?foo=bar&baz=baz. Don't confuse this with the PHP variables that start with a $.
5

Remove the $ before errorMssg and urlencode the message.

Comments

1

could it be because you have $errorMssg instead of $errorMsg ? also try to make a valid url, for example replace " " with %20 etc, function urlencode() could help you with this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.