Jump to content

Values of variables lost after form post


drgrumpy

Recommended Posts

This is my first post, so please forgive me if I fail to follow conventions and/or am a bit wordy. I've been struggling with this for a week of late nights/early mornings and unable to find what I am doing wrong...

 

Using PHP v5.2.1 on linux/apache test server

 

I am trying to create a 'simple' paypal payments system which requires the user to be logged in:

 

I post the order form with buy now button to process.php script:

 

<form method='post' action='../includes/process.php'>

<input type='hidden' name='amount' value='4.95'>

<input type='hidden' name='item_name' value='Item 1'>

<input type='image' src='../images/buynow.gif' name='submit'>

</form>

 

process.php first assigns the $_POST values to an array to pass to paypal:

<?

$paypal['item_name']= $_POST['item_name'];

$paypal['amount']= $_POST['amount'];

$paypal['amount2'] = 4.95;

?>

then includes login.php which checks if the user is logged in and if not presents a simple login form ...

then returns to process.php which posts the required $paypal array values to paypal via a hidden form.

 

This all works as expected if the user is already logged in (so login.php doesn't output any html), but if the user has to login (i.e. submits another form) the values of the $paypal array which were assigned to the original $_POST array values are lost, but values which are assigned a value are retained, so item_name and amount above are lost and amount2 is retained (I put in amount2 just to test that it is not a scope issue, and I have tried enclosing the $_POST variables in quotes, etc.) Of course I expect the values to the $_POST array to get unset, but this behaviour makes me think the the assignments are actually references to the $POST values and not copies as intended.

 

I read that new in php 5 the default method of assignment of objects is by reference and that clone() can be used to force a copy BUT I can't find anything about the equivalent for variables, and if I try to use:

<?

$paypal['item_name']= clone($_POST['item_name']);

?>

I get a warning Warning: __clone method called on non-object

so how do I force a copy of a variable if as it seems assignment is now always by reference ? or am I missing something more fundamental or is it a bug ?

 

Many thanks for your help,

 

Steve

 

 

Link to comment
https://forums.phpfreaks.com/topic/97169-values-of-variables-lost-after-form-post/
Share on other sites

clone is not what you are looking for...

 

you need to use SESSIONS to store the information before forwarding them to login.php, then read that info from the SESSION once returned to process.php

 

Here is a tutorial on SESSIONS: http://devzone.zend.com/node/view/id/646

Thanks for reply.

 

I tried using sessions already i.e.

 

$_Session['paypal'] = $paypal;

 

do login checks

 

$paypal = $_Session['paypal']

 

But this still doesn't work because it the values of the $_POST array are still assigned as references and so get reset if there is a form post during login.  So what I need is a way to make the original assignment copy the values.

The method is

 

process.php

<?php
session_start();
if($_SERVER['REQUEST_METHOD'] == 'POST'){
  $paypal['item_name']= $_POST['item_name'];
  $paypal['amount']= $_POST['amount'];
  $paypal['amount2'] = 4.95;
}elseif($_SESSION['paypal']){
  $paypal = $_SESSION['paypal'];
  unset($_SESSION['paypal']);
}else{
  die("No data");
}
if(!$logged_in){
  $_SESSION['paypal'] = $paypal;
  header('Location: login.php');
  exit;
}
//Continue using $paypal

 

login.php just has to send the user to process.php after login

 

Update: $logged_in is something I made up...you seemed like you already had the code to check if a user is logged in, so make sure you change that to what you have already.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.