PHPagi Conditional If

Hi

I’m trying the Conditional If passing variables from asterisk (Elastix) to phpagi and viceversa and consulting to Mysql DB, I dont know what It wrong with the code, In asterisk I do the following:
Dial 75 then dial 4 digits (pin) that code is sent to phpagi that makes a query in a Mysql DB to see if the the PIN exists in DB, depending If the PIN exists or not I create a variable for making another If conditional (this second if is not working):

This is Dialplan code:
;Test If
exten => 75,1,Answer
exten => 75,n,Read(pin,enter-password,4)
exten => 75,n,AGI(testif.php,${pin})
exten=> 75,n,NoOp(${pinName})
exten=> 75,n,NoOp(${pinNameif})
same => n,Hangup

This is agiphp code:
[b]#!/usr/bin/php -q

<?php require ('phpagi.php') ; //definir operador de agi agiwrapper $agiwrapper = new AGI() ; ob_implicit_flush(true) ; set_time_limit(30) ; //Variable from Sterisk $pinR = $_SERVER['argv'][1]; $agiwrapper->set_variable(VarNopin,"nopin"); //Mysql Conn. $con = mysql_connect("localhost","root","logramsa"); if (!$con) { die('Could not connect: ' . mysql_error()); } //Select DB mysql_select_db("pbx", $con); //Do query $query = 'SELECT * FROM pins WHERE pin = '.$pinR.' LIMIT 0, 30 '; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array( $result ); //Firts If check If PIN exist if($row != NULL) { $result = $row[0]; //Save PIN name $agiwrapper->set_variable(pinName,$result); } else { //If the PIN is not in DB $agiwrapper->set_variable(pinName,"nopin"); } //Second If if($pinName == "nopin") { $agiwrapper->set_variable(pinNameif,"pinNoFound"); } else { $agiwrapper->set_variable(pinNameif,"pinfound"); } //Cerrar Sesion mysql_close($link); ?>[/b]

This the mysql table (pins)
[b]mysql> select * from pins;

±------±-----+
| name | pin |
±------±-----+
| peter | 1234 |
| joe | 4321 |
±------±-----+[/b]

This what asterisk console shows when I dial PIN: 1234, this PIN exists in the DB and the first If shows me the PIN name “peter” the second If show “pinfound”, thats ok:

– Executing [75@from-internal:1] Answer(“SIP/8000-00000086”, “”) in new stack
– Executing [75@from-internal:2] Read(“SIP/8000-00000086”, “pin,enter-password,4”) in new stack
– Accepting a maximum of 4 digits.
– <SIP/8000-00000086> Playing ‘enter-password.gsm’ (language ‘en’)
– User entered ‘1234’
– Executing [75@from-internal:3] AGI(“SIP/8000-00000086”, “testif.php,1234”) in new stack
– Launched AGI Script /var/lib/asterisk/agi-bin/testif.php
– <SIP/8000-00000086>AGI Script testif.php completed, returning 0
[color=#0000FF] – Executing [75@from-internal:4] NoOp(“SIP/8000-00000086”, “peter”) in new stack
– Executing [75@from-internal:5] NoOp(“SIP/8000-00000086”, “pinfound”) in new stack[/color]
– Executing [75@from-internal:6] Hangup(“SIP/8000-00000086”, “”) in new stack

But If I dial I PIN that is not in the DB the first If works fine shows “nopin”, but the second If shows the same message as is The PIN exists “pinfound”, I dont what is wrong with the second If:

– Executing [75@from-internal:1] Answer(“SIP/8000-00000087”, “”) in new stack
– Executing [75@from-internal:2] Read(“SIP/8000-00000087”, “pin,enter-password,4”) in new stack
– Accepting a maximum of 4 digits.
– <SIP/8000-00000087> Playing ‘enter-password.gsm’ (language ‘en’)
– User entered ‘5864’
– Executing [75@from-internal:3] AGI(“SIP/8000-00000087”, “testif.php,5864”) in new stack
– Launched AGI Script /var/lib/asterisk/agi-bin/testif.php
– <SIP/8000-00000087>AGI Script testif.php completed, returning 0
[color=#0040FF] – Executing [75@from-internal:4] NoOp(“SIP/8000-00000087”, “nopin”) in new stack
– Executing [75@from-internal:5] NoOp(“SIP/8000-00000087”, “pinfound”) in new stack[/color]
– Executing [75@from-internal:6] Hangup(“SIP/8000-00000087”, “”) in new stack

Please any Ideas?

Allltough the second if-condition in Your php-script is a bit insane as it could be handled within the first condition …

Why would You expect php to know something about a (php)variable $pinName which is not set to any value within php in any way ? Namely You’re setting up a variable pinName which sounds like it could be a php-one, bit You’re doing this encapulated in a $agiwrapper->set_variable - Call which just sets up this variable within thecalling Asterisk-Context (and Asterisk therefor has access to the value) but not within php !

Thanks for your answer abw1oim, I’m trying If conditional because I want to be familiar with it, I’m going to use If conditional in another phpagi program that is going to make some calculation according the data from a query.
But What can I do to use the variable “$pinName” ($agiwrapper->set_variable(pinName,“nopin”):wink: from the query (first If) in the the second If php code, I Thought variables declares in $agiwrapper->set_variable can be use as php code.

Thanks again abw1oim, I didnt know that Variables has to be get and set for being use in php and asterisk, I used get_variable and set_variable to solve the problem, this the code workig:

[b]#!/usr/bin/php -q

<?php require ('phpagi.php') ; //definir operador de agi agiwrapper $agiwrapper = new AGI() ; ob_implicit_flush(true) ; set_time_limit(30) ; //Variable from Asterisk $pinR = $_SERVER['argv'][1]; $agiwrapper->get_variable("pinR", $pinR); $agiwrapper->set_variable(VarNopin,"nopin"); //Mysql Conn. $con = mysql_connect("localhost","root","logramsa"); if (!$con) { die('Could not connect: ' . mysql_error()); } //Select DB mysql_select_db("pbx", $con); //Do query $query = 'SELECT * FROM pins WHERE pin = '.$pinR.' LIMIT 0, 30 '; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array( $result ); //If PIN exist if($row != NULL) { $result = $row[0]; //Save PIN name $pinName=$result; } else { //If the PIN is not in DB $pinName="nopin"; } if($pinName == "nopin") { $pinNameif="pinNoFound"; } else { $pinNameif="pinFound"; } $agiwrapper->set_variable("pinName",$pinName); $agiwrapper->set_variable("pinNameif",$pinNameif); $agiwrapper->verbose("The result was: $pinName", 3); $agiwrapper->verbose("The result was if: $pinNameif", 3); //Cerrar Sesion mysql_close($link); ?>[/b]