[asterisk-users] Need help with PHP script to authenticate user from database

Steve Edwards asterisk.org at sedwards.com
Mon Sep 15 15:02:21 CDT 2008


On Mon, 15 Sep 2008, Zeeshan Zakaria wrote:

> I am trying to write a basic AGI script in PHP to authenticate a user from
> database. So far I have no luck. Need help. Maybe somebody can share their
> code. Here is what I have:
>
> extension.conf
> ---------------------
>
> [database-lookup]
> exten => _.,1,Answer()
> exten => _.,n,AGI(lookup.agi)
> exten => _.,n,NoOp(${AUTH})
> exten => _.,n,Hangup
>
>
> lookup.agi
> ---------------
>
> !/usr/bin/php -q
> <?php
> require '/var/lib/asterisk/agi-bin/phpagi.php';
> $agi = new AGI();
>
> $no=preg_replace("#[^0-9]#","",$agi->request[agi_callerid]);
>
> $db = 'db1';
> $dbuser = 'user1';
> $dbpass = 'password';
> $dbhost = 'localhost';
>
> mysql_connect($dbhost,$dbuser,$dbpass);
> mysql_select_db("$db");
> $row=mysql_query("select cid from callcheap_users where cid = '$no'");
> if (mysql_num_rows($row)==1)
> {
>        $row=mysql_fetch_array($row);
>        if ($row[cid1]) $auth = "YES";
> }
>        else $auth="NO";
> $agi->set_variable("lookupcid", $auth);
> ?>
>
>
> As per my understanding, the variable 'lookupcid' should have the cid
> assigned from the database and show it in the NoOp command, but I get the
> following:
>
>    -- Executing AGI("IAX2/4124874318-1", "lookup.agi") in new stack
>    -- Launched AGI Script /var/lib/asterisk/agi-bin/lookup.agi
>    -- AGI Script lookup.agi completed, returning 0
>    -- Executing NoOp("IAX2/4124874318-1", "") in new stack
>    -- Executing Hangup("IAX2/4124874318-1", "") in new stack

Casual inspection says you are setting a variable named "lookupcid" but 
you are "noop-ing" a variable named AUTH.

When debugging AGIs, "agi debug" (1.2) is your friend.

Thanks in advance,
------------------------------------------------------------------------
Steve Edwards      sedwards at sedwards.com      Voice: +1-760-468-3867 PST
Newline                                             Fax: +1-760-731-3000



More information about the asterisk-users mailing list