By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,825 Members | 1,364 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,825 IT Pros & Developers. It's quick & easy.

help needed posting to a URL

P: 4
I've created a mod for processing information posted by a return function from an online payment site. After It processes the information It must post some variables to another mod. I'm just starting in PHP programming and cant find a way for doing that without creating a form and forcing the user to click on a button.

Let's say my mod is called return.php and receives a number of posted fields from the payment link return function. This mod processes that information and creates two variables called $_ord_id (string) and $_purchased (yes/no).
After processing that infromation it must automaticaly redirect to a URL like http://mysite.com/mod.php?mod=orders&mode=return posting those two variables to the URL.

Now the question: What would be the code to do that redirection and post those variables?

Thanks for the help.

Airton
Feb 10 '07 #1
Share this Question
Share on Google+
7 Replies


ronverdonk
Expert 2.5K+
P: 4,258
Welcome to The Scripts!

You show a lot of text, but where is your code? If you want us to help you, show the code you have developed so far and we will have a look and try to see what you need to achieve your goal.

Ronald :cool:
Feb 11 '07 #2

P: 99
the solution can be simple if you use a session.
[PHP]
if(count($_POST) > 0){
// process the form
// put variables in the session
$_SESSION['ord_id'] = $_ord_id;
$_SESSION['purchased'] = $_purchased;
// redirect to other page
header('http://mysite.com/mod.php?mod=orders&mode=return');
}
[/PHP]

If you do this it's best you include these pages in a master page and you start the session there. For security reasons you can unset the session variables when you finished processing them on the redirected page.
Feb 11 '07 #3

P: 4
Sorry guys for not having posted my code. But now, here it goes:

Whem my client finishes the payment process at the on-line payment site it will return automaticaly to this script by calling the URL http://mysite.com/return.php and posting a number of values to it.

[PHP]// RECEIVES THE POST SENT BY BRPAY AND VALIDATE THE DATA
$BRpay = 'Comando=validar';
$BRpay .= '&Token=1234567890';
$Cabecalho = "";

foreach ($_POST as $key => $value){
$value = urlencode(stripslashes($value));
$BRpay .= "&$key=$value";
}

if (function_exists('curl_exec')){
$curl = true;
}
elseif ( (PHP_VERSION >= 4.3) && ($fp = @fsockopen('ssl://www.brpay.com.br', 443, $errno, $errstr, 30)) ){
$fsocket = true;
}
elseif ($fp = @fsockopen('www.brpay.com.br', 80, $errno, $errstr, 30)){
$fsocket = true;
}

// SEND DATA BACK TO BRPAY FOR VALIDATION
if ($curl == true){
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://www.brpay.com.br/Security/NPI/Default.aspx/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $BRpay);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($ch);
if (!tep_not_null($resp)){
curl_setopt($ch, CURLOPT_URL, 'http://www.brpay.com.br/Security/NPI/Default.aspx/');
$resp = curl_exec($ch);
}

curl_close($ch);
$confirma = (strcmp ($resp, "VERIFICADO") == 0);
}
elseif ($fsocket == true){
$Cabecalho = "POST /Security/NPI/Default.aspx HTTP/1.0\r\n";
$Cabecalho .= "Content-Type: application/x-www-form-urlencoded\r\n";
$Cabecalho .= "Content-Length: " . strlen($BRpay) . "\r\n\r\n";

if ($fp || $errno>0){
fputs ($fp, $Cabecalho . $BRpay);
$confirma = false;
$resp = '';
while (!feof($fp)){
$res = @fgets ($fp, 1024);
$resp .= $res;
// Verifies if the transaction status is VERIFICADO (VERIFIED)
if (strcmp ($res, "VERIFICADO") == 0){
confirma=true;
break;
}
}
fclose ($fp);
}
}

if (confirma){
$_purchased = 'yes';
}
else{
$_purchased = 'no';
}[/PHP]

At this point the script will process the posted fields and may or may not change the $_purchased value depending on the results (under development). The value for $_ord_id is also posted to this script.
After the processing the script must redirect to http://mysite.com/mod.php?mod=orders&mode=return posting $_purchased and $_ord_id to that URL. This is the part of the code that I need.
All the user interaction is done by this last URL.

Thanks for the help so far.

Airton
Feb 12 '07 #4

P: 4
I've made some changes to my script and it's not working. I tried to use xwero's sugestion bu all I get is a blank page when the pay-link returns to my mod and it doesn't redirects to the URL it should. What coud be wrong?

Thanks for the help.

Airton

Here is the modified code:

[PHP]<?php
// RECEBE O POST ENVIADO PELA BRPAY E ADICIONA OS VALORES PARA VALIDAÇÃO DOS DADOS
$BRpay = 'Comando=validar';
$BRpay .= '&Token=123456789';
$Header = '';
$confirma = false;

foreach ($_POST as $key => $value){
$value = urlencode(stripslashes($value));
$BRpay .= "&$key=$value";
}

if ( (PHP_VERSION >= 4.3) && ($fp = @fsockopen ('ssl://www.brpay.com.br', 443, $errno, $errstr, 30)) ){
$fsocket = true;
}
elseif ($fp = @fsockopen('www.brpay.com.br', 80, $errno, $errstr, 30)){
$fsocket = true;
}

// ENVIA DE VOLTA PARA A BRPAY OS DADOS PARA VALIDAÇÃO
if ($fsocket == true){
$Header = "POST /Security/NPI/Default.aspx HTTP/1.0\r\n";
$Header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$Header .= "Content-Length: " . strlen($BRpay) . "\r\n\r\n";

if ($fp || $errno>0){
fputs ($fp, $Header . $BRpay);
$resp = '';
while (!feof($fp)){
$res = @fgets ($fp, 1024);
$resp .= $res;
if (strcmp ($res, "VERIFICADO") == 0){
$confirma=true;
break;
}
}
fclose ($fp);
}
}
$_ord_id= $_POST['Referencia'];
if ($Confirma){
$_Status = 'OK';
}
else{
$_Status = 'NOK';
}

// REDIRECIONA PARA A FUNCAO DE RETORNO
$_SESSION['ord_id'] = $_ord_id;
$_SESSION['Status'] = $_Status;
// redirect to other page
header('http://www.hotspothosting.com.br/mod.php?mod=orders&mode=return');
?>[/PHP]
Feb 12 '07 #5

P: 99
Where did you start your session?

For the session option to work you need to embed the page you got in a master page which handles the redirections. And in that page you start your session so you can get and set session variables for all the pages that are embedded.

a quick an dirty redirection masterpage example

[PHP]
// cache get variables for checking
$getParameter1 = $_GET['parameter1'];
// checking code here
// page redirection based on first parameter
switch($getParameter1){
case 'a':
// cache get variables after first parameter for checking
$getParameter1_1 = $_GET['parameter1_1'];
// checking code here
// page redirection
include('page1.php')
break;
}
[/PHP]
if that page is named index.php all the urls are index.php?... The url to get you to page1.php is index.php?parameter1=a&parameter1_1=b.

You can have as many levels as you want ofcourse.
Feb 13 '07 #6

P: 99

[PHP]<?php

header('http://www.hotspothosting.com.br/mod.php?mod=orders&mode=return');
?>[/PHP]
should be

[PHP]<?php

header('Location: http://www.hotspothosting.com.br/mod.php?mod=orders&mode=return');
?>[/PHP]
Feb 13 '07 #7

P: 4
Thank you guys for your help.
Talking to the developer of my shoping cart I got to know that I could use GET to pass the values to the mod, so I changed the last part of my code (based on the tips from xwero) as follows:

[PHP]$_ord_id= $_POST['Referencia'];
if ($Confirma){
$_Status = 'OK';
}
else{
$_Status = 'NOK';
}

// REDIRECTS TO phpCoin RETURN FUNCTION
$Header = 'Location: http://www.hotspothosting.com.br/mod.php?mod=orders&mode=return&ord_id=';
$Header .= $_ord_id;
$Header .= '&Status=';
$Header .= $_Status;

// redirect to other page
header($Header);
[/PHP]

Now it's working as expected.
Thanks again.
Cheers :)

Airton
Feb 13 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.