Forum Moderators: coopster

Message Too Old, No Replies

pass variable to another page

         

kristof_v

12:57 pm on Nov 21, 2006 (gmt 0)

10+ Year Member



hi,

i know i can pass a variable with POST or GET.
but i want to do the following:

a user enters wrong login credentials, so i redirect the user to error.php.
but i also want to pass the message "login credentials not found!".
I could do this with: header("location:index?php?page=error&err=wrong login credentials");

but i don't want the error string to be visible.
can i do this another way?
POST doesn't seem an option to me as there isn't a form involved.

grtz

barns101

1:26 pm on Nov 21, 2006 (gmt 0)

10+ Year Member



Just put an error number in the URL and then on the next page use if() or switch() to display the correct error message that corresponds to the error number in the URL.

kristof_v

1:33 pm on Nov 21, 2006 (gmt 0)

10+ Year Member



smart thinking!

but just out of curiosity, isn't there also a possibility to pass a variable, say like this:

$err_msg = "blablabla";
header("location:index.php?page=error&msg=err_msg");

kristof_v

2:10 pm on Nov 21, 2006 (gmt 0)

10+ Year Member



that would also be nice for this scenario:

mysql_query(...) or die(mysql_error());
so yould could pass mysql_error() to error.php and show the error there.

like: mysql_query(...) or header("location:index.php?page=error&err=mysql_error()");

thecoolone

2:23 pm on Nov 21, 2006 (gmt 0)

10+ Year Member



u cud create a session variable as
$_SESSION['errmsg']="the error msg u want"

and in the next page u can use this same session variable
$msg=$_SESSION['errmsg']

echo "the error u wanted to display"

kristof_v

2:44 pm on Nov 21, 2006 (gmt 0)

10+ Year Member



that's right

thx!

pixeltierra

4:56 am on Nov 22, 2006 (gmt 0)

10+ Year Member



A session var is the best thing in this case, I think, if it's like a redirect.

Other ways of passing invisible data would be with a hidden form element.

It seems like there should be a php function that lets you send post data to a page like this:

post_to_url ($url, $data);

But I can't think of anything off the top of my head. Anyone?

pixeltierra

5:01 am on Nov 22, 2006 (gmt 0)

10+ Year Member



I've just found this on a zend forum. Poster was greg8872 (for credit). I can't vouch for it in any way.

// Prepare string to be sent as POST data
// Add the values into array $pData
// The formatted post string will be in the
// variable $pString

$pData = array();
$pString = '';

$pData['x_login'] = 'username';
$pData['x_password'] = 'password';
$pData['x_tran_key'] = 'keyvalue';
$pData['x_cardnum'] = '4111111111111111';
$pData['x_exp_date'] = '1108';
$pData['x_amount'] = '24.95';

foreach($pData as $key => $val)
$pString .= trim($key) . '=' . urlencode(trim($val)) . "&";
$pString = substr($pString,0,-1);

// Access remote site with the post data
// The resulting web page SOURCE CODE will be
// in the variable $aResult

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://secure.authorize.net/gateway/transact.dll");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pString);

$aResult = curl_exec($ch);
curl_close($ch);