Trying to run a cgi script from a php script.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

greree
Forum Newbie
Posts: 15
Joined: Sun Dec 14, 2003 10:34 am

Trying to run a cgi script from a php script.

Post by greree »

I'm trying to intergrate a cgi affiliate tracking system into a php
website. When someone pays using PayPal it's supposed to direct them to one
page if the payment is a success or another if payment failed. This is the
code:

Code: Select all

if($action == order && $order_n != "")
	{

		include "engine/card_process.pml";

		if($payment_status == success)
		{
			$page = payment_success;
		}
		else
		{
			$page = payment_failed;
		}
	}
I'm trying to insert code into this process that will credit the affiliate
the correct amount if the process is a success. This is the code that's
supposed to be inserted to credit affiliates:

Code: Select all

<img src="http://www.yoursite.com/cgi-bin/affiliates/sale.cgi?payment=
$amount" border=0>
I tried inserting this before the line "$page = payment_success;" in
various forms, but I either get errors, or the affiliate isn't credited, or
both. I managed to get the code to credit affiliates correctly, by
inserting this:

Code: Select all

include "<img src='http://www.yoursite.com/cgi-bin/affiliates/sale.cgi?
payment=$amount' border=0>";
but it also gives me errors:

Code: Select all

Warning: main(): failed to open stream: No such file or directory in 
/home/httpd/vhosts/yoursite.com/httpdocs/index.php on line 18

Warning: main(): Failed opening '<img src='http://www.yoursite.com/cgi-
bin/affiliates/sale.cgi?payment=0.01' border=0>' for inclusion 
(include_path='.:/usr/share/pear') in 
/home/httpd/vhosts/yoursite.com/httpdocs/index.php on line 18

Warning: session_start(): Cannot send session cookie - headers already sent 
by (output started at 
/home/httpd/vhosts/yoursite.com/httpdocs/index.php:18) in 
/home/httpd/vhosts/yoursite.com/httpdocs/index.php on line 29

Warning: session_start(): Cannot send session cache limiter - headers 
already sent (output started at 
/home/httpd/vhosts/yoursite.com/httpdocs/index.php:18) in 
/home/httpd/vhosts/yoursite.com/httpdocs/index.php on line 29

Warning: Cannot modify header information - headers already sent by (output 
started at /home/httpd/vhosts/yoursite.com/httpdocs/index.php:18) in 
/home/httpd/vhosts/yoursite.com/httpdocs/index.php on line 69
Line 18 is the inserted code, line 29 is:

session_start();

Line 69 is:

setcookie("current_page"' $page);

What I'm looking for is the proper syntax for the line of code that credits affiliates. If you can help me I would appreciate it. I really don't have a clue about what I'm doing. I'm just guessing. I look to see how something is done in another part of the script and try to adapt it, but in this case I'm stumped. Thanks.
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

You don't want this:

Code: Select all

include "<img src='http://www.yoursite.com/cgi-bin/affiliates/sale.cgi? 
payment=$amount' border=0>";
You want this:

Code: Select all

echo "<img src="http://www.yoursite.com/cgi-bin/affiliates/sale.cgi? 
payment=$amount" border="0">";
greree
Forum Newbie
Posts: 15
Joined: Sun Dec 14, 2003 10:34 am

Post by greree »

Ok, the "Failed to open stream" error went away, but the "session_start(): Cannot send session cookie" errors are still there. Thanks for the help so far.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

something is printing out before session_start() is hit.. this can literally be anything.. blank lines.. html..
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

That means that you're attempting a header action (which includes session_start) AFTER you've done output to the screen. All header actions must be done before you attempt to write anything to screen.


// or maybe I should double-check before I post to see if someone else got there first :)
greree
Forum Newbie
Posts: 15
Joined: Sun Dec 14, 2003 10:34 am

Post by greree »

If I take out the line for the affiliate credit it works perfectly. When I put it back in I get the error. So it must be something in the line I inserted.

This the entire file:

Code: Select all

<?
    require "includes/load_configuration.pml";
    require "includes/sched.pml";
    
    session_cache_limiter('none');
    
    $timestamp=time();
    $timeout=$timestamp-$timeoutseconds;

    $db = c();
	if($action == order && $order_n != "")
	{

		include "includes/card_process.pml";

		if($payment_status == success)
		{
                  echo "<img src="http://www.yoursite.com/cgi-bin/affiliates/sale.cgi?payment=$fOrder[amount]" border="0">";
			$page = payacc;

		}
		else
		{
			$page = payrej;
		}
	}

	include "includes/signin_pages.pml";
    session_start();
	//  Locating current page

	if($page == logout)
	{
        q("DELETE FROM dp_usersonline WHERE userid='$sAuth'");
		$page = index;
		$sAuth = "";

		setcookie("sAuth");
	}

    if ($profile_in_one_step)
    {
    if ($page == "create_prof" && $login != "" && $pswd != "")
    {
        $fMember = f(q("select * from dp_members where login='$login' and pswd='$pswd'"));

        if($fMember[ id ] != "")
        {
			setcookie("sAuth", $fMember[ id ]);
			$sAuth = $fMember[ id ];
        }
    }
    }
    
	if(IsRequiredLogin($page) && $sAuth == "")
	{
		$page = login;
	}

	if(!isset($page) && !isset($current_page))
	{
	 	$current_page = index;
	}

	if(!empty($page))
	{
        if ($page != "view_prof")
        {
	 	     setcookie("current_page", $page);
        }
		$current_page = $page;
	}



	$logged_in = 0;

	//	Handling actions

	if($action == login)
	{
		if($login == "" || $pswd == "")
		{
			$error = "Invalid username or password entered!";
		}
		else
		{
			$fMember = f(q("select * from dp_members where login='$login' and pswd='$pswd'"));

			if($fMember[ id ] == "")
			{
				$error = "The username or password you entered is incorrect.";
			}
			else
			{
				setcookie("sAuth", $fMember[ id ]);
				$sAuth = $fMember[ id ];

				$logged_in = 1;
                q("UPDATE dp_profile set lastlogin=".time()." WHERE member_id=".$fMember[id]);
			}
		}
	}

	if(!$logged_in && isset($sAuth))
	{
		$fMember = f(q("select * from dp_members where id='$sAuth'"));

		if($fMember[ id ] == "")
		{
			setcookie("sAuth");
		}
		else
		{
			$logged_in = 1;
		}
	}

	//	EOF Handling actions

	if($current_page == index && $logged_in)
	{
		$current_page = mem_homepage;
	}
	if($current_page == login && $logged_in)
	{
		$current_page = mem_homepage;
	}



    $ses_id = session_id();

	if($logged_in)
	{
		$member_code = sysGetProfileCode();

                $fExists = f(q("SELECT id from dp_usersonline where session_id='$ses_id'"));
                if ($fExists[id] == "")
                {
                   q("INSERT INTO dp_usersonline (timestamp, ip, login, userid, session_id) VALUES ($timestamp,'$REMOTE_ADDR','$fMember[login]', $fMember[id], '$ses_id')");
                }
                else
                {
                   q("UPDATE dp_usersonline set timestamp=$timestamp, login = '$fMember[login]', userid = $fMember[id] where session_id='$ses_id'");
                }
    }
    else
    {
                $fExists = f(q("SELECT id from dp_usersonline where session_id='$ses_id'"));
                if ($fExists[id] == "")
                {
                   q("INSERT INTO dp_usersonline (timestamp, ip, login, session_id) VALUES ($timestamp,'$REMOTE_ADDR','', '$ses_id')");
                }
                else
                {
                   q("UPDATE dp_usersonline set timestamp=$timestamp where session_id='$ses_id'");
                }
    }
    q("DELETE FROM dp_usersonline WHERE (timestamp<$timeout)");
    $rUsers = q("select DISTINCT ip from dp_usersonline where (login = '')and(timestamp>$timeout)");
    $guestson = (int)nr($rUsers);

    $rUsers = q("select DISTINCT userid, login from dp_usersonline where (login <> '')and(timestamp>$timeout)");
    $registeredon = (int)nr($rUsers);


    include "templ/page_header.ihtml";
    include "includes/pmls/$current_page.pml";
    include "templ/page_footer.ihtml";

	d($db);
?>

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

{ 
                  echo "<img src="http://www.yoursite.com/cgi-bin/affiliates/sale.cgi?payment=$fOrder[amount]" border="0">"; 
         $page = payacc; 

      }
it's your echo.

from what I can guess.. you should be able to move session_start() up to the top of the file, or just after session_cache_limiter()
greree
Forum Newbie
Posts: 15
Joined: Sun Dec 14, 2003 10:34 am

Post by greree »

I moved session_start() to after session_cache_limiter(). Now I'm getting this error.

Code: Select all

Warning: Cannot modify header information - headers already sent by (output started at /home/httpd/vhosts/yoursite.com/httpdocs/index.php:18) in /home/httpd/vhosts/yoursite.com/httpdocs/index.php on line 68
Line 68 is setcookie("current_page", $page);
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

session_start should be one of the very first things you call in your script - setting a cookie is modifying the header information, you cannot then set a session after it. Keep juggling that line around.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there must be something printing out before session_start() gets hit still.. check your included/required files for any text that prints out.
greree
Forum Newbie
Posts: 15
Joined: Sun Dec 14, 2003 10:34 am

Post by greree »

But it works fine if I comment out the echo "affiliate credit line" on line 18.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

The second you echo something you're also echoing out a whole load of header information at the same time - if this happens before any other PHP function that requires to be able to modify the header (such as a session or cookie) then you get the Headers error you're receiving.
greree
Forum Newbie
Posts: 15
Joined: Sun Dec 14, 2003 10:34 am

Post by greree »

Ok, the original line I need to insert is

Code: Select all

<img src="http://www.yoursite.com/cgi-bin/affiliates/sale.cgi?payment=
$fOrder&#1111;amount]" border=0>
I need it to run after PayPal sends the member back to the site after he's paid. I also need it to pick up the payment amount variable. Is there a way to modify the line to get rid of the error?
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

The line isn't the problem - it's the fact you are trying to set a cookie (or session) after it that causes the error. What you want to achieve will work, you just need to get things in the right order.
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

First of all, copying your code from above, it starts with this:

Code: Select all

<?
That's a line break and a space before the <?, which counts as HTML output. I don't know if that's actually in your code, but if it is you have to get rid of it.

If that's not it, then what's on Line 18?
Post Reply