Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP passing a variable with header redirect

Tags:

php

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?

like image 738
somdow Avatar asked Apr 06 '12 20:04

somdow


3 Answers

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'];
like image 144
Benjamin Crouzier Avatar answered Oct 06 '22 00:10

Benjamin Crouzier


Remove the $ before errorMssg and urlencode the message.

like image 37
kontur Avatar answered Oct 05 '22 23:10

kontur


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.

like image 36
mkk Avatar answered Oct 06 '22 01:10

mkk