Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable undefined error

Status
Not open for further replies.

inusrat

Programmer
Feb 28, 2004
308
CA
Hi,

I am trying to hook up my site to paypal. paypal wants to use the following code. But it is giving me this error, I don't know why

Notice: Undefined variable: header in
C:\Inetpub\ on
line 13

where line 13 is
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";

I don't know why it is saying variable undefined

where line 13 is
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";

here is part of the paypal code

****************
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-synch';

$tx_token = $_GET['tx'];
$auth_token = "GX_sTf5bW3wxRfFEbgofs88nQxvMQ7nsI8m21rzNESnl_79ccFTWj2aPgQ0";
$req .= "&tx=$tx_token&at=$auth_token";

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen (' 80, $errno, $errstr, 30);
***********************************

 
Is the variable $header initialized anywhere before you get to line 13? If not change the ".=" to "=". Or initialize $header.

Code:
$header = '';
// rest of Paypal code
$req = 'cmd=_notify-synch';

$tx_token = $_GET['tx'];
$auth_token = "GX_sTf5bW3wxRfFEbgofs88nQxvMQ7nsI8m21rzNESnl_79ccFTWj2aPgQ0";
$req .= "&tx=$tx_token&at=$auth_token";

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-[URL unfurl="true"]www-form-urlencoded\r\n";[/URL]
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('[URL unfurl="true"]www.paypal.com',[/URL] 80, $errno, $errstr, 30);

Ken
 
Thanks . Their code should be all tested, i don't know why it is causing this problem. I will try the suggestions you guys have given.....

Here is the full code

****
<?php
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-synch';

$tx_token = $_GET['tx'];

$auth_token = "U9ki_Q_3BcBVti61nFNHr5KHudpGu4SzxOjIS9gr0nGY55kcSiWtVX58qQe";

$req .= "&tx=$tx_token&at=$auth_token";


// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen (' 80, $errno, $errstr, 30);
// If possible, securely post back to paypal using HTTPS
// Your PHP server will need to be SSL enabled
// $fp = fsockopen ('ssl:// 443, $errno, $errstr, 30);

if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
// read the body data
$res = '';
$headerdone = false;
while (!feof($fp)) {
$line = fgets ($fp, 1024);
if (strcmp($line, "\r\n") == 0) {
// read the header
$headerdone = true;
}
else if ($headerdone)
{
// header has been read. now read the contents
$res .= $line;
}
}

// parse the data
$lines = explode("\n", $res);
$keyarray = array();
if (strcmp ($lines[0], "SUCCESS") == 0) {
for ($i=1; $i<count($lines);$i++){
list($key,$val) = explode("=", $lines[$i]);
$keyarray[urldecode($key)] = urldecode($val);
}
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment
$firstname = $keyarray['first_name'];
$lastname = $keyarray['last_name'];
$itemname = $keyarray['item_name'];
$amount = $keyarray['payment_gross'];

echo ("<p><h3>Thank you for your purchase!</h3></p>");

echo ("<b>Payment Details</b><br>\n");
echo ("<li>Name: $firstname $lastname</li>\n");
echo ("<li>Item: $itemname</li>\n");
echo ("<li>Amount: $amount</li>\n");
echo ("");
}
else if (strcmp ($lines[0], "FAIL") == 0) {
// log for manual investigation
}

}

fclose ($fp);

?>

Your transaction has been completed, and a receipt for your purchase has been emailed to you.<br>You may log into your account at <a href=' to view details of this transaction.<br>
***********


 
PHP is giving you a NOTICE. Read about the error reporting settings and the different levels. You can safely suppress notices.
Check what "they" say about the settings your PHP installation should have to use their code without any notices/errors.
 
I think you are right. I don't get this error when I put the same code on one hosting company's server. So you are saying, Code is OK, I just need to suppres the notices. I installed PHP on my machine. Now where do I need to go to change the settings and suppres notices?

 
Now wait a minute. Notices are useful, too, and simply supressing them out-of-hand does not improve your code.

The problem is this line:

$header [red].=[/red] "POST /cgi-bin/webscr HTTP/1.0\r\n";

At the time this line is run, $header doesn't exist, so the ".=" string append operator has nothing to append to. PHP will create the variable and then perform the append, but it's giving you that notice to let you know you're doing something unwise.

As the line of code above is the first time this script references header, just use an assignment operator rather than an append operator:

$header [red]=[/red] "POST /cgi-bin/webscr HTTP/1.0\r\n";


Why go about reconfiguring PHP when simply removing one character will fix the problem?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
When I put the above mentioned code on my home server i get value for $lines[0], though the value is "FAIL". which is OK. But when I put the same code on one of the hosting company (Dynonames) none of the if statements get executed.

*********
if (strcmp ($lines[0], "SUCCESS") == 0) {
for ($i=1; $i<count($lines);$i++){
list($key,$val) = explode("=", $lines[$i]);
$keyarray[urldecode($key)] = urldecode($val);
}
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment
$firstname = $keyarray['first_name'];
$lastname = $keyarray['last_name'];
$itemname = $keyarray['item_name'];
$amount = $keyarray['payment_gross'];

echo ("<p><h3>Thank you for your purchase!</h3></p>");

echo ("<b>Payment Details</b><br>\n");
echo ("<li>Name: $firstname $lastname</li>\n");
echo ("<li>Item: $itemname</li>\n");
echo ("<li>Amount: $amount</li>\n");
echo ("");
}
else if (strcmp ($lines[0], "FAIL") == 0) {
// log for manual investigation
}

}
************

Any idea why it is lke that? If i try to print echo "$lines[0]"; nothing gets printed.
Thanks

 
You are probably dealing with a register_globals issue.
Check out the phpinfo() output on your system and compare it to the output from the host. The host probably (and rightfully so) set register_globals OFF, which has been default since I believe PHP 4.1.2
 
i use printinfo() to compare.

register_globals are "off" on my machine and "On" my hosting company. My version is 4.3.7 and their's is 4.3.8

Someone from Paypal forum suggested i should check the rights on the folder where I have my PHp code. I checked the rights, I have given max right on the folder where I have my php code..

so rights should not be a problem either...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top