Can php sessions wipe out a post variable?
Results 1 to 9 of 9

Thread: Can php sessions wipe out a post variable?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    19

    Question Can php sessions wipe out a post variable?

    Hi all,
    I'm Leoa and I'm very very stuck on this script. I want to add a POST variable, $_POST['h2'], to a php page. I've narrowed down the problem to this section of code below. Basically, when I remove this piece of code I am able to echo the POST variable on the page. When put this code back on the page, the POST variable is gone. I get an error of "index undefined. " I have searched the reason for this error, and in this case it is not the php.ini file error settings. I need to find a way to get the posted variable on this page (GET does not work either).The $_POST['pid'] post works in this script.
    PHP Code:
    if (isset($_POST['h2'])){
        echo 
    $_POST['h2'];
        }
        if (isset(
    $_POST['pid'])){
           
           
    $pid $_POST['pid'];
           
           
    $i=0;
           
           
    $wasFound false;
           
           
    // if not set, or cart array is empty
           
           
    if (!isset($_SESSION['cart_array']) || count($_SESSION['cart_array'])<1){
             
              
    $_SESSION['cart_array'] = array( => array('item_id'=>$pid'quantity'=> 1));
             
             
           }else{
              
    // at least one item was found in the cart
              
    foreach($_SESSION ['cart_array'] as $each_item){
                 
                 
    $i++;// hold value of which associative array  with in mulitdeminalt array is pasing thru loop
                 
                 
    while(list($key,$value) = each($each_item)){
                    
    //while loop access to all key value pairs in cart_array
                    //list() Like array(), this is not really a function, but a language construct. list() is used to assign a list of variables in one operation.
                    //each()Return the current key and value pair from an array and advance the array cursor.
                 
                   
                    
    if($key == "item_id" && $value ==$pid){
                       
                       
    // that item is in cart already so lets adjust it quantity using array_splice();
                       // add one to that item
                       ///array_splice remove portion of array and replace it with something else
                       
                       //input, offset, index, replacement
                       
    array_splice($_SESSION['cart_array'], $i-11, array(array("item_id" =>$pid"quantity"=> $each_item['quantity'] +1)));
                       
                       
    $wasFound =true;
                    }
                   
                 }
             
              }
             
              if(
    $wasFound == false){
                 
                 
    array_push($_SESSION['cart_array'], array('item_id'=>$pid'quantity'=> 1));
                 
    print_r($_SESSION['cart_array']);
                 
                 
              }
             
           }
           
    header("location:cart.php");
           exit();
        }

        
    ?> 
    Questions:
    1. Could the Session variables remove the post variable?
    2. Is the multidimensional array taking the all the Posted variables? I made the multidimensional array specify for variable $_POST['pid']. Is this array also taking the variable $_POST['h2'] . Could this happen through the Session variable?


    Please Help!

  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    19
    Full code here:

    Code:
    
    <?php
    ////////////// Section 1 establish cart
    
    echo $_POST['h2'];
    
    if (isset($_POST['pid'])){
    	
    	$pid = $_POST['pid'];
    	
    	$i=0;
    	
    	$wasFound = false;
    	
    	// if not set, or cart array is empty 
    	
    	if (!isset($_SESSION['cart_array']) || count($_SESSION['cart_array'])<1){
    		
    		$_SESSION['cart_array'] = array( 0 => array('item_id'=>$pid, 'quantity'=> 1));
    		
    		
    	}else{
    		// at least one item was found in the cart
    		foreach($_SESSION ['cart_array'] as $each_item){
    			
    			$i++;// hold value of which associative array  with in mulitdeminalt array is pasing thru loop
    			
    			while(list($key,$value) = each($each_item)){
    				//while loop access to al key value pairs in cart_array
    				//list() Like array(), this is not really a function, but a language construct. list() is used to assign a list of variables in one operation. 
    				//each()Return the current key and value pair from an array and advance the array cursor. 
    			
    				
    				if($key == "item_id" && $value ==$pid){
    					
    					// that item is in cart already so lets adjust it quanity using araay_splice();
    					// add one to that item
    					///array_splice remove portion of array and replace it with someting else
    					
    					//input, offset, index, replacemnt
    					array_splice($_SESSION['cart_array'], $i-1, 1, array(array("item_id" =>$pid, "quantity"=> $each_item['quantity'] +1)));
    					
    					$wasFound =true;
    				}
    				
    			}
    		
    		}
    		
    		if($wasFound == false){
    			
    			array_push($_SESSION['cart_array'], array('item_id'=>$pid, 'quantity'=> 1));
    			print_r($_SESSION['cart_array']);
    			
    			
    		}
    		
    	}
    	header("location:cart.php");
    	exit();
    }
    
    ?>
    
    
    <?php
    
    
    ////////////// Section 2 (if user choose to empty their Shopping cart)
    
    
    if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart"){
    	
    	unset($_SESSION['cart_array']);
    }
    
    
    ?>
    
    
    
    
    
    
    <?php
    
    
    
    
    ////////////// Section 4 remove items from cart
    
    
    if(isset($_POST['index_to_remove']) && $_POST['index_to_remove']!=""){
    	
    	//access the array and run code to remove that array index
    	
    	$key_to_remove=''.mysql_real_escape_string(htmlentities($_POST['index_to_remove'])).'';
    	
    	echo 'index : '.$key_to_remove.': Count- <br>';
    	
    	if(count($_SESSION['cart_array'])<=1){
    		
    		
    		unset($_SESSION['cart_array']);
    		
    		
    		
    	}else{
    		
    		unset($_SESSION['cart_array'][$key_to_remove]);
    		
    		sort($_SESSION['cart_array']);
    		
    		echo count ($_SESSION['cart_array']);
    		
    	}
    	
    }
    
    
    ?>
    
    <?php
    
    
    if (isset($_POST['index_to_change']) && $_POST['index_to_change'] != ""){
    	echo $_POST['index_to_change'];
    	
    	 $index_to_change = $_POST['index_to_change'];
    	
    	$quantity = preg_replace('#[^0-9]#i','',$_POST['quantity'] );
    	
    	if($quantity >= 100){
    		
    		$quantity=99;
    	}
    	
    	
    	if($quantity <1){
    		
    		$quantity=1;
    	}
    	$i=0;
    	
    	foreach($_SESSION ['cart_array'] as $each_item){
    			
    			$i++;// hold value of which associative array  with in mulitdeminalt array is pasing thru loop
    			
    			while(list($key,$value) = each($each_item)){
    				//while loop access to al key value pairs in cart_array
    				//list() Like array(), this is not really a function, but a language construct. list() is used to assign a list of variables in one operation. 
    				//each()Return the current key and value pair from an array and advance the array cursor. 
    			if($key == "item_id" && $value ==$index_to_change){
    					
    					// that item is in cart already so lets adjust it quanity using araay_splice();
    					// add one to that item
    					///array_splice remove portion of array and replace it with someting else
    					
    					//input, offset, index, replacemnt
    					array_splice($_SESSION['cart_array'], $i-1, 1, array(array("item_id" =>$index_to_change, "quantity"=> $quantity)));
    					
    					
    				}
    			
    			}
    	}
    	
    }
    
    
    ?>
    
    
    <?php
    
    
    ////////////// Section 5 render the cart to view
    
    
    $cartOutput="";
    $cartTotal ="";
    $priceTotal="";
    $pp_checkout_btn="";
    $product_id_array="";
    
    
    if(!isset($_SESSION['cart_array']) || count($_SESSION['cart_array'] )<1){
    	
    	$cartOutput ="<h2 align='center> Your Shopping Cart is empty</h2>";
    	
    }else{
    	
    	$pp_checkout_btn.='<form action ="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_cart">
    <input type="hidden" name="upload" value="1">
    <input type="hidden" name="business" value="webmaster@leobee.com">';
    	
    	$product_id_array.=$item_id."-".$each_item['quantity'].";";
    	// product id - price 
    	
    	$i =0;
    	
    	foreach($_SESSION['cart_array'] as $each_item){
    		
    			$item_id=$each_item['item_id'];
    	
    			$query = "SELECT * FROM products WHERE id=$item_id LIMIT 1";
    			
    			echo $query ."<br>";
    	
    			$resutls=mysql_query($query);
    			
    			$product_name="";
    			$price="";
    			
    			while ($row= mysql_fetch_array($resutls))	{
    				$item_id = $row['id'];
    				
    				$product_name=$row['product_name'];
    				
    				$price = $row['price'];
    				
    				$details= $row['details'];
    				
    				
    			}
    			$cartTotal = $priceTotal + $cartTotal;
    			$priceTotal= $price * $each_item['quantity'];
    			$x=$i+1;
    			$pp_checkout_btn.='
    <input type="hidden" name="item_name_'.$x.'" value="'. $product_name.'">
    <input type="hidden" name="amount_'.$x.'" value="'.$price.'">
    <input type="hidden" name="quantity_'.$x.'" value="'.$each_item['quantity'].'">';
    
    //query database to update amount of product in inventory.( may need to use hidden value in usein inventory var name)
    			
    			$cartOutput.= '<tr valign="top">';
    			$cartOutput.= '<td> '. $product_name.'<a href="product.php?id='.$item_id.'"> <br/> <img src="http://localhost/phpTutorials/ecomsite/storeadmin/inventory_images/'.$item_id.'.jpg"></a></td>';
    			$cartOutput.= '<td>'.$details.' </td>';
    			$cartOutput.= '<td> $'.$price.' </td>';
    			
    			$cartOutput.='<td><br><form method="post" action="cart.php"><input type ="text" size="1" maxlength="2" name="quantity" value="'. $each_item['quantity'].'"/><input name="changeBtn"'.$item_id.'" type="submit" value="Change"/><input name="index_to_change" type="hidden" value="'.$item_id.'"></form> </td>';
    			
    			$cartOutput.= '<td>'.$each_item['quantity'].' </td>';
    			$cartOutput.= '<td>'.$priceTotal.' </td>';
    			
    			
    			$cartOutput.='<td><br><form method="post" action="cart.php"><input name="deleteBtn"'.$item_id.'" type="submit" value="X"/> <input name="index_to_remove" type="hidden" value="'.$i.'"></form> </td>';
    			$cartOutput.= '</tr>';
    			
    				$i++;
    				
    		}
    //https://p8.secure.hostingprod.com/@leobee.com/ssl/index.php
    	$pp_checkout_btn.='<input type="hidden" name="custom" value="'.$product_id_array.'">
    	<input type="hidden" name="notify_url" value="http://localhost/phpTutorials/ecomsite/storescripts/my_ipn.php">
    <input type="hidden" name="return" value="http://localhost/phpTutorials/ecomsite/storescripts/checkout_complete.php">
    <input type="hidden" name="rm" value="2">
    <input type="hidden" name="cbt" value="Return to Store">
    <input type="hidden" name="cancel_return" value="http://localhost/phpTutorials/ecomsite/storescripts/paypal_cancel.php">
    <input type="hidden" name="lc" value="US">
    <input type="hidden" name="currency_code" value="XXXXX">
    <input type="image" name="submit"  src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" alt="Make payment with PayPal">
    </form>';
    }
    
    ?>
    
    <!DOCTYPE html>
    
    <html lang="en">
    
    <head>
    
    	<meta charset="utf-8">
    	
    	<link rel="stylesheet" href="style/ecom.css">
    	
    </head>
    
    <body>
    	
    	<div id="page_wraper">
    		
    		<?php include_once("config/template_header.php")?>
    		
    			<section>
    				
    				<article>
    					
    					    <section id="main" class="flexbox">
    					  <div id="pageContent">
    					  	<div style="margin:24px; text-align:left;">
    					  	
    					  		<br/><a href="index.php"> continue shopping</a><br/>
    					  		<table border="1" background-color="white" cellpadding="6" cellspacing="6">
    					  			
    					  			<tr>
    					  				<td> Product</td>
    					  				<td> Product Description</td>
    					  				<td> Unit Price</td>
    					  				<td> Quanity</td>
    					  				<td> Total</td>
    					  				<td>Remove</td>
    					  			</tr>
    					  			
    					  		
    					  			
    					  				<?php echo $cartOutput;?>
    					  		</table>
    					  	<div align="right">	<?php echo "Cart Total: $".$cartTotal." USD "?></div><br>
    					  	<div align="right">	<?php echo "Purchase:".$pp_checkout_btn?></div><br>
    					  		<a href="cart.php?cmd=emptycart">Click here to empty your cart</a>
    					  	</div>
    					  	<br/>
    					  </div>
       						 </section>
       						 
    				</article>
    		
    			</section>
    			
    		<?php include_once("config/template_footer.php")?>
    			
    	</div>
    	
    </body>
    
    </html>

  3. #3
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,429
    Welcome to PHPBuilder!

    Quote Originally Posted by Leoa View Post
    1. Could the Session variables remove the post variable?
    Variables aren't living/sentient beings - they can't do anything on their own. So the answer is, no, simply using a session variable doesn't somehow modify the $_POST array.

    Quote Originally Posted by Leoa View Post
    2. Is the multidimensional array taking the all the Posted variables?
    What do you mean by "taking" them? The best answer I can give is... there's no magic behind PHP code; it does exactly what it says. So if the foreach() loop (that is what we're talking about, right?) somehow references (or "takes" - whatever that means) all of the variables you expect to be POST'ed, then yes. Otherwise, no.

    Quote Originally Posted by Leoa View Post
    I made the multidimensional array specify for variable $_POST['pid']. Is this array also taking the variable $_POST['h2'] .
    Again - what does "taking" a variable even mean? Also again - the code does what it says it does, so does $_POST['h2'] appear anywhere inside the relevant section of the code? If so, then yes. Otherwise, no.

    Quote Originally Posted by Leoa View Post
    Could this happen through the Session variable?
    You can only access $_POST['h2'] from a session variable if you first store the value of $_POST['h2'] in a session variable.

    Also, as a generic debugging technique, try doing a print_r() on the entire $_POST array to see if it contains what you think it contains.

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    19

    Can php sessions wipe out a post variable?

    The form is on a different page called product.php. This page is in the same directory as cart.php. cart.php is the file that the $_POST, variable $_POST['h2'], does not populate. However $_POST[pid'] does populate. Both variables are coming from the same form.


    Code:
    	 
    <form id="form1" name="form1" method="POST" action="cart.php">	
         <input type="hidden" name="pid" id="pid" value="<?php echo $id;?>"/>
         <input type="hidden" name="h2" id="h2" value="pancakes"/>
         <input type="submit" name="submit" id="submit" value="Add to Shopping Cart">
     </form>

    I've tried this code on the cart.php page and there is nothing in the POST array
    Code:
    <?php
    
    print_r($_POST);
    
    var_dump($_POST);
    
    ?>
    results:Array ( ) array(0) { }

    If I remove this code below from cart.php the variables post to the page.
    Code:
    <?php 
    if (isset($_POST['pid'])&& !empty($_POST['pid'])){
    	
    	$pid = $_POST['pid'];
    	
    	$i=0;
    	
    	$wasFound = false;
    	
    	// if not set, or cart array is empty 
    	
    	if (!isset($_SESSION['cart_array']) || count($_SESSION['cart_array'])<1){
    		
    		$_SESSION['cart_array'] = array( 0 => array('item_id'=>$pid, 'quantity'=> 1));
    		
    		
    	}else{
    		
    		// at least one item was found in the cart
    		foreach($_SESSION ['cart_array'] as $each_item){
    			
    			$i++;// hold value of which associative array  with in mulitdeminalt array is pasing thru loop
    			
    			while(list($key,$value) = each($each_item)){
    				//while loop access to al key value pairs in cart_array
    				//list() Like array(), this is not really a function, but a language construct. list() is used to assign a list of variables in one operation. 
    				//each()Return the current key and value pair from an array and advance the array cursor. 
    			
    				
    				if($key == "item_id" && $value ==$pid){
    					
    					// that item is in cart already so lets adjust it quanity using araay_splice();
    					// add one to that item
    					///array_splice remove portion of array and replace it with someting else
    					
    					//input, offset, index, replacemnt
    					array_splice($_SESSION['cart_array'], $i-1, 1, array(array("item_id" =>$pid, "quantity"=> $each_item['quantity'] +1)));
    					
    					$wasFound =true;
    				}
    				
    			}
    		
    		}
    		
    		if($wasFound == false){
    			
    			array_push($_SESSION['cart_array'], array('item_id'=>$pid, 'quantity'=> 1));
    			print_r($_SESSION['cart_array']);
    			
    			
    		}
    		
    	}
    	header("location:cart.php");
    	exit();
    }
    
    ?>

  5. #5
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,429
    Quote Originally Posted by Leoa View Post
    I've tried this code on the cart.php page and there is nothing in the POST array
    Code:
    <?php
    
    print_r($_POST);
    
    var_dump($_POST);
    
    ?>
    results:Array ( ) array(0) { }
    That would suggest that you didn't POST any data to cart.php when you received that output. So, after adding that code, how did you test it? Did you fill out the form you showed above and submit it, or did you go directly to cart.php?

    Quote Originally Posted by Leoa View Post
    If I remove this code below from cart.php the variables post to the page.
    Not really; either a given a request for cart.php contains POST'ed data along with the request or it doesn't. The contents of cart.php have no bearing on the matter at all - by the time its contents even come into play, PHP has already processed the request and filled in the $_POST array appropriately.

  6. #6
    Junior Member
    Join Date
    Feb 2012
    Posts
    19
    Quote Originally Posted by bradgrafelman View Post
    That would suggest that you didn't POST any data to cart.php when you received that output. So, after adding that code, how did you test it? Did you fill out the form you showed above and submit it, or did you go directly to cart.php?

    Not really; either a given a request for cart.php contains POST'ed data along with the request or it doesn't. The contents of cart.php have no bearing on the matter at all - by the time its contents even come into play, PHP has already processed the request and filled in the $_POST array appropriately.

    bradgrafelman, thanks for following this thread.

    I check the post request through fiddler and cart.php is receiving the posted variables.

    http://leobee.com/sugardefynery/erro...dvariables.jpg

    However they vars don't display on the page:
    http://leobee.com/sugardefynery/erro...snotonpage.jpg

  7. #7
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,429
    In the first image, the screenshot would appear to show the print_r() contents to the right. In it, the 'h2' is indeed present as expected.

    However, the response code for that request would appear to be 302, which is a temporary redirect code. The second screen shot is from the next request, so it should make sense that $_POST is empty for that request since you didn't POST anything in that request (instead you're simply following a redirect).

  8. #8
    Junior Member
    Join Date
    Feb 2012
    Posts
    19
    I'm not sure why the page is redirecting because there isn't any redirect code that runs when the page loads.

    However, the variables are reaching the page and then the redirect happens. I can assume that if I put the $_POST variable in a $_SESSION that the data from the $_POST variable will be accessible on the cart.php page after the redirect?

    Do you have any suggestions?

  9. #9
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,885
    Quote Originally Posted by Leoa
    I'm not sure why the page is redirecting because there isn't any redirect code that runs when the page loads.
    PHP Code:
           header("location:cart.php");
           exit(); 
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •