Page 1 of 2

how to pass the variable through php header.

Posted: Thu May 06, 2010 6:36 am
by amitdubey2
hello everyone.

i am using header to redirect a page to another.I also want to pass a variable $cid through this page..which i am getting from the previous page.
i m using this syntax for header page.. :arrow:

Code: Select all

<?
	include("includes/db.php");
	include("includes/functions.php");
	
	
	$cid = $_GET['cid'];

	echo $cid;
   
        if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
		$pid=$_REQUEST['productid'];
		addtocart($pid,1);
		header("Location:shoppingcart.php?cid=" . $cid);
		exit();
	}
?>





but i am nt getting $cid value in url for next page..can anyone point out wht should be the correct syntax for that..?? :?: :?:


Regards
Amit :idea:

Re: how to pass the variable through php header.

Posted: Thu May 06, 2010 7:26 am
by timWebUK
You should be using the absolute URL in your header.

header('Location:http://www.example.org/path/to/file');

Re: how to pass the variable through php header.

Posted: Thu May 06, 2010 7:28 am
by amitdubey2
i am attaching all three files..
1.Catagory.php (first page)
2.Products.php (Second page)
3.Shoppingcart.php(Third page)


i am passing the cid from the catagory.php page to products.php page and from that passing through header to shoppingcart.php.

Re: how to pass the variable through php header.

Posted: Thu May 06, 2010 7:32 am
by amitdubey2
hello timWebUK

i dont think the problem is because of absolute path ..because if i m nt sending any variable through header header is going to the desired page...only problem is it is nt taking variable value through header...

Plz check the attached files in my previous post.
I am also going to trying absolute path for that..as you said.



Regards
Amit :crazy:

Re: how to pass the variable through php header.

Posted: Thu May 06, 2010 7:43 am
by amitdubey2
i dont knw exactly ...but the problem is in products.php page...
in the statement
$cid =$_get['cid']; // 3rd statement

because when i am directly assigning some value like...

$cid = 2; //like that

it is working absolutely fine...
but with that statement i am nt able to get the $cid value in next page...

intrestengly when i echo the $cid ...
$cid =$_get['cid'];
echo $cid;

it is giving the correct output...


Any Help??? :?: :?: :idea:



Regards
Amit :crazy:

Re: how to pass the variable through php header.

Posted: Thu May 06, 2010 8:29 am
by timWebUK

Code: Select all

<?php

//This page redirects you using header() and passes a GET value with it

$param = $_GET['id'];
header("Location: http://www.example.org/path/to/file.php?param=$param");

?>
This should work.

Re: how to pass the variable through php header.

Posted: Thu May 06, 2010 11:58 pm
by amitdubey2
hello timWebUK,

This code is similar to the code which i have done and it is also giving the same in url as $param=.........(blank).It is passing only name nt the value for $param variable.. :?: :?: :?: :?:

Code: Select all

<?php

//This page redirects you using header() and passes a GET value with it

$param = $_GET['id'];
header("Location: http://www.example.org/path/to/file.php?param=$param");

?>
I knw it is write code to use for header...but i dont knw why it is nt working with this script. :( :( :(

In other scripts it is working absolutely fine..( i have checked earlier)
Please check the attachment and assest me if you can ...thank you. :wink:



Regards
Amit

Re: how to pass the variable through php header.

Posted: Fri May 07, 2010 12:21 am
by amitdubey2
hello timWebUK,

I think the problem is that i am using a function addtocart()..for this script..so when i press add to cart button it calls the addtocart function so it is nt taking the value inside the function and redirect to other page without that...
i am attaching the function script..please go through that also ..and tell me wht should be the modification so that it will take value in the header.
function code.

Code: Select all

<?
	function get_product_name($pid){
		$result=mysql_query("select name from products where serial=$pid");
		$row=mysql_fetch_array($result);
		return $row['name'];
	}
	function get_price($pid){
		$result=mysql_query("select price from products where serial=$pid");
		$row=mysql_fetch_array($result);
		return $row['price'];
	}
	function remove_product($pid){
		$pid=intval($pid);
		$max=count($_SESSION['cart']);
		for($i=0;$i<$max;$i++){
			if($pid==$_SESSION['cart'][$i]['productid']){
				unset($_SESSION['cart'][$i]);
				break;
			}
		}
		$_SESSION['cart']=array_values($_SESSION['cart']);
	}
	function get_order_total(){
		$max=count($_SESSION['cart']);
		$sum=0;
		for($i=0;$i<$max;$i++){
			$pid=$_SESSION['cart'][$i]['productid'];
			$q=$_SESSION['cart'][$i]['qty'];
			$price=get_price($pid);
			$sum+=$price*$q;
		}
		return $sum;
	}
	function addtocart($pid,$q){
		if($pid<1 or $q<1) return;
		
		if(is_array($_SESSION['cart'])){
			if(product_exists($pid)) return;
			$max=count($_SESSION['cart']);
			$_SESSION['cart'][$max]['productid']=$pid;
			$_SESSION['cart'][$max]['qty']=$q;
		}
		else{
			$_SESSION['cart']=array();
			$_SESSION['cart'][0]['productid']=$pid;
			$_SESSION['cart'][0]['qty']=$q;
		}
	}
	function product_exists($pid){
		$pid=intval($pid);
		$max=count($_SESSION['cart']);
		$flag=0;
		for($i=0;$i<$max;$i++){
			if($pid==$_SESSION['cart'][$i]['productid']){
				$flag=1;
				break;
			}
		}
		return $flag;
	}

?>

regards
amit :crazy:

Re: how to pass the variable through php header.

Posted: Fri May 07, 2010 12:49 am
by flying_circus
You should not use PHP short tags. use "<?php" instead of "<?"

You should also, at the very least, check a variables existence before referencing it.

Code: Select all

<?php
	include("includes/db.php");
	include("includes/functions.php");
	
	
	$cid = isset($_GET['cid']) ? $_GET['cid'] : '';

	echo $cid;
   
        if($_REQUEST['command']=='add' && isset($_REQUEST['productid']) && $_REQUEST['productid']>0){
		$pid=$_REQUEST['productid'];
		addtocart($pid,1);
		header("location:shoppingcart.php?cid={$cid}");
		exit();
	}
?>

Re: how to pass the variable through php header.

Posted: Fri May 07, 2010 2:04 am
by amitdubey2
hello flying_circus ,

i have tried all the ways..(also the one you suggested)...but unfortunetly i am nt getting the value.. although it echo the value outside the function call...
bt inside call to addtocart function it is nt able to even echo the value for $cid...which i think is the main problem ...
so it is nt able to send the value from the header to next page.. :?: :?:
Please check ...and suggest me ,how to solve this problem.... 8O 8O



regards
amit

Re: how to pass the variable through php header.

Posted: Fri May 07, 2010 2:16 am
by cwheel3915

Code: Select all

<?php
        include("includes/db.php");
        include("includes/functions.php");
        
        
        $cid = isset($_GET['cid']) ? $_GET['cid'] : '';

        echo $cid;
   
        if($_REQUEST['command']=='add' && isset($_REQUEST['productid']) && $_REQUEST['productid']>0){
                $pid=$_REQUEST['productid'];
                addtocart($pid,1);
                $url = "shoppingcart.php?cid=" . $cid;
                echo("<script>
<!--

               location.replace(\"" . $url . "\");
-->
</script>");


               
        }
?>


Give that a try.

Re: how to pass the variable through php header.

Posted: Fri May 07, 2010 2:23 am
by Apollo
amitdubey2 wrote:although it echo the value outside the function call...
bt inside call to addtocart function it is nt able to even echo the value for $cid...which i think is the main problem ...
In all your code above, you didn't use $cid inside a function anywhere. So are you sure you're doing exactly what you describe here?

Anyway, based on your description, I think your problem is with $cid's variable scope. This should work:

page1.php:

Code: Select all

<?php
$cid = 'hello';
header("Location: page2.php?cid=".$cid); // redirect to page2.php
?>
page2.php:

Code: Select all

<?php
function Bla()
{
  global $cid; // note this line!!!
  echo "cid is now: $cid";
}
$cid = $_GET['cid'];
Bla();
?>
But this only works as long as you use the global $cid; declaration in your function. If you omit that, it will NOT work (because $cid is then a local variable inside the function).

Re: how to pass the variable through php header.

Posted: Fri May 07, 2010 2:43 am
by amitdubey2
hello all,,

anyone there to take me out from this muddy problem..???
i have tried all the methods suggested above ... bt nthing is working for my problem..
i knw that problem is nt with the redirection of the header..bt with the value coming from $cid=$_GET['cid'].
Because when i am assigning some value directly ( $cid=1or 2 something like that) it is working...
So wht to do knw ... :?: :?: :?: :banghead: :banghead: :banghead:
Any idea Anyone...:idea: :idea: :idea:


Regards
Amit

Re: how to pass the variable through php header.

Posted: Fri May 07, 2010 3:00 am
by amitdubey2
Hello Apollo ,

I think i am nt quite clear...
here i am showing wht i was saying...(please go through the comments inside the php code)

when using $cid= 1(like that...)... It is working properly and passing the value through url.

Code: Select all

<?php
	include("includes/db.php");
	include("includes/functions.php");
		
$cid= 1;    //It is working properly with the hardcoded value 1(or 2 something like that)
echo $cid;   //here it is echoing the value 1
   
   //Function call to addtocart()
    
 if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
		$pid=$_REQUEST['productid'];
		addtocart($pid,1);

          echo $cid  // Here it is  echoing the value 1

        header("Location: http://localhost/shopping/shoppingcart.php?cid=$cid");
		exit();
 }
	  
?>





When using the $cid =$_Get['cid']....It is not working properly and nt passing the value through url..

Code: Select all

<?php
	include("includes/db.php");
	include("includes/functions.php");
		
$cid= $_GET['cid'];      //it is not working
echo $cid;                 // Here it is echoing the value  for $cid   

        if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
		$pid=$_REQUEST['productid'];
		addtocart($pid,1);

echo $cid;               // Here it is not echoing the value for $cid

        header("Location: http://localhost/shopping/shoppingcart.php?cid=$cid");
		exit();
 }
	  
?>


Now i think it is understandable....



regards
amit

Re: how to pass the variable through php header.

Posted: Fri May 07, 2010 5:18 am
by Apollo
Ok, interesting. I've added some debug output to that last code you posted. What happens when you try this:

Code: Select all

<?php
include("includes/db.php");
include("includes/functions.php");

$cid= $_GET['cid']; //it is not working
$ci2= 1;

echo '<br>1a: '.$cid; // Here it is echoing the value  for $cid
echo '<br>1b: '.$ci2;

if ($_REQUEST['command']=='add' && $_REQUEST['productid']>0)
{
	echo '<br>2a: '.$cid; // Is it still echoing the value for $cid here??
	echo '<br>2b: '.$ci2;

	$pid=$_REQUEST['productid'];

	echo '<br>3a: '.$cid; // Is it still echoing the value for $cid here??
	echo '<br>3b: '.$ci2;

	addtocart($pid,1);

	echo '<br>4a: '.$cid; // Is it still echoing the value for $cid here??
	echo '<br>4b: '.$ci2;

	header("Location: http://localhost/shopping/shoppingcart.php?cid=$cid"); // (this won't work anymore because you have echo'd stuff)
	exit();
}
echo 'if you see this, no redirection would take place at all (neither with nor without $cid)';
?>