Anari Sengbe
Anari Sengbe

Reputation: 81

Calling 2 Different Databases

I'm trying to have the PAYPAL IPN work with 2 different sites, I can't get the php script to know which database to work with. Here's my script.

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// post back to PayPal system to validate
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

// If testing on Sandbox use:
//$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);


// assign posted variables to local variables
$custom = $_POST['item_name'];

//Config file for 1st database
if($custom=="1"){
require_once("../config.php");

//Script to execute

}

 //Config file for 2nd database (different site)
if($custom=="2"){
require_once("../config.php");

//Script to execute
}

Upvotes: 2

Views: 76

Answers (2)

Anari Sengbe
Anari Sengbe

Reputation: 81

I found a great script for this that worked great. It automatically send the paypal response to the correct site based on the email.

http://www.scriptomart.com/view-item/15-Paypal-IPN-Multiple-Email-Accounts.html is the script but the one I bought wasn't free so you may have to look for that one. I can't find the exact link.

Upvotes: 0

SeanCannon
SeanCannon

Reputation: 78046

Your code is making a lot of assumptions and isn't very defensive. It's assuming the form was posted, that it was successfully populated, that $_POST['item_name'] will contain only the strings '1' or '2' and that your script is running in a path which is one folder deep to a directory containing config.php...and that regardless of the condition, the same config.php will be loaded and miraculously know what it needs to do.

What would make more sense is something like this:

try
{
    if(isset($_POST['site_name']))
    {
        require_once('../configs/' . $_POST['site_name'] . '/config.php');
        if(!isset($some_var_declared_inside_config_file))
        {
            throw new Exception('Config not loaded');
        }
    }
    else
    {
        throw new Exception('Site name not provided');
    }
}
catch(Exception $e)
{
    echo $e->getMessage();
}

Then you could have your configs setup like so:

/www/paypal.php
/configs/sitea/config.php
/configs/siteb/config.php
/configs/sitec/config.php

Upvotes: 3

Related Questions