[Date Prev][Date Next] [Chronological] [Thread] [Top]

Re: problems with PHP search



Tom wrote: 
> Hello All,
> I'm doing some PHP pages in order to access an LDAP server. I want to
> do a search on sn and givenname.

An "and" filter. Okay.

> Look at this :
>      My filter :
>         $filter  ="(sn=*". $sn ."*)";
>         $filter .="(givenname=*". $gn ."*)";
>      My result :
>         If $sn="que" the result is :
>            name      = queste
>            givenname = thomas
>         if $sn="que" AND $gn="mas" the result is :
>            name      =
>            givenname = thomas
> In fact, if I specify two arguments in my filter ($sn and $gn), the
> search result miss some results (like the name in this example).
> I think that my filter isn't good. Must I do something like this :
> $filter  ="&(sn=*". $sn ."*)";
> $filter .=&"(givenname=*". $gn ."*)";
> (in order to make an "AND" operation).

One way to do it, written in php:
<?php

$build_filter = "";
$num_of_AND = 0;

if ($sn){
    $build_filter  ="(sn=*". $sn ."*)";
    $num_of_AND++;
    }

if ($gn){
    $build_filter  ="(givename=*". $sn ."*)";
    $num_of_AND++;
    }

if ($user){
    $build_filter  ="(uid=*". $user ."*)";
    $num_of_AND++;
    }

if ($num_of_AND > 1){
    $filter_for_search = "(&" . $build_filter . ")";
    } else {    \\ if there is only one search term
    $filter_for_search = $build_filter;
    }
?>

This way, you can have one AND(OR), or thirty, and change the |
or the & when you are ready building the whole string.

HTH,
-Ronabop
--
Personal:  ron@opus1.com, 520-326-6109, http://www.opus1.com/ron/
Work: rchmara@pnsinc.com, 520-546-8993, http://www.pnsinc.com/
The opinions expressed in this email are not necessarily those of myself,
my employers, or any of the other little voices in my head.