Jump to content

Pioden

Members
  • Posts

    72
  • Joined

  • Last visited

    Never

Posts posted by Pioden

  1. Just figured out the three dimensional thingy.

     

    echo $info["0"]["uid"]["0"];

     

    gives me exactly the data I need - but just for the first array. How do I loop it through the arrays to get the results?

     

    foreach {

    echo $info["0"]["uid"]["0"];

    }

     

    doesn't work. I guess I need some statement before {

     

    BTW I really do appreciate your patience. I'm used to working with database results so this kind of array stuff is unknown territory!

  2. Wow. Thank you both - I think I'm getting somewhere now.

     

    Using Gareth's suggestion I now see:

    Array
    (
        [count] => 2
        [0] => Array
            (
                [uid] => Array
                    (
                        [count] => 1
                        [0] => user1
                    )
    
                [0] => uid
                [count] => 1
                [dn] => uid=user1,ou=Users,dc=mycollege,dc=ac,dc=uk
            )
    
        [1] => Array
            (
                [uid] => Array
                    (
                        [count] => 1
                        [0] => user2
                    )
    
                [0] => uid
                [count] => 1
                [dn] => uid=user2,ou=Users,dc=mycollege,dc=ac,dc=uk
            )
    
    )
    

     

    using foreach

    (an example dragged kicking and screaming from the manual comments)

     

    foreach ($info as $v1) {

        foreach ($v1 as $v2) {

            echo "$v2<br />";

        }

    }

     

    the output is:

    Warning: Invalid argument supplied for foreach() in /var/www/huw/ldaptest.php on line 38

    Array

    uid

    1

    uid=user1,ou=Users,dc=mycollege,dc=ac,dc=uk

    Array

    uid

    1

    uid=user2,ou=Users,dc=mycollege,dc=ac,dc=uk

     

    I'm a bit puzzled why it warns about invalid arguments and then prints a result though!!

     

    Can I use foreach to just access the userx value in array uid?

  3. Hi folks,

     

    As a results of an ldap search I have an array (or maybe more than one) which I can't figure out how to process.

     

    The output of print_r ($info); (where ifo is the array) gives me two results which look like this:

     

    Array ( [count] => 2 [0] => Array ( [uid] => Array ( [count] => 1 [0] => user1 ) [0] => uid [count] => 1 [dn] => uid=user1,ou=Users,dc=mycollege,dc=ac,dc=uk ) [1] => Array ( [uid] => Array ( [count] => 1 [0] => user2 ) [0] => uid [count] => 1 [dn] => uid=user2,ou=Users,dc=mycollege,dc=ac,dc=uk ) ) 

     

    Processing the arrays with array_values

    $prawf = array_values($info);
    while (list($key,$value) = each($info)) {
    echo "$key : $value<br />";
    }

     

    gets me

     

    count : 2

    0 : Array

    1 : Array

     

    which to me suggests arrays within arrays. How on earth do I process this stuff to get meaningful values i.e. user1,user2 etc.

     

    All assistance very much apperiated.

     

    TIA

     

    Huw

  4. OK. I'm still hoping for replies - but I've also made a little progress!

     

    I've managed to filter out most of the info I don't need by modifying the query with some filtering

     

    $justthese = array("uid", "departmentnumber", "maillocaladdress"); 
    
    $search =  ldap_search($ds, "dc=mycollege,dc=ac,dc=uk", "uid=$username", $justthese);
    
    $info = ldap_get_entries($ds, $search);
    
    print_r ($info);
    

     

    This gives me all the data I need - but with a whole bunch of array info. Here's a snippet.

     

    Array ( [count] => 2 [0] => Array ( [uid] => Array ( [count] => 1 [0] => username1 ) [0] => uid [maillocaladdress] => Array ( [count] => 1 [0] => useremail ) [1] => maillocaladdress [departmentnumber] => Array ( [count] => 1 [0] => Engineering ) [2] => departmentnumber [count] => 3 [dn] => uid=username,ou=Users,dc=mycollege,dc=ac,dc=uk ) 

     

    So the follow on question is how do I turn this into something useful? Is this a job for 'foreach' ? Sorry my brain is pretty fried at the moment!!

     

  5. LDAP questions don't seem to get many responses here but undeterred I'm going to give it a go!

     

    I'm writing a little Ajax/PHP combo to search our college (LDAP) address book. I'm slowly getting to grips with the AJAX bits but the LDAP searching is giving me a serious headache!

     

    My code at the moment returns the entire LDAP record for the users it finds. Reams of stuff!!  But all I really need is  "departmentnumber" and "maillocaladdress" for matches to "uid".

     

    The equivalent SQL query would be simple:

    SELECT departmentnumber,maillocaladdress,uid FROM ldap WHERE uid = searchname

    (something like that anyway!)

     

    Can anybody point me in the right direction with filtering the LDAP search results? The stuff I've found on-line is confusing to say the least.

     

    TIA

     

    Huw

     

    <?php
    
    
    
    
    
    $username = "huw.*";
    
    
    
    // LDAP variables
    
    $ldaphost = "ldapslave.mycollege.ac.uk";  // your ldap servers
    
    $ldapport = 389;                 // your ldap server's port number
    
    
    
    // Connecting to LDAP
    
    $ds = ldap_connect($ldaphost, $ldapport) or die ("Could not connect to $ldaphost");
    
    
    
    //Connection made -- bind anonymously and get dn for username.
    
        $bind = @ldap_bind($ds);
    
        
    
    //Check to make sure we're bound.
    
        if( !bind )
    
        {
    
            echo "Anonymous bind to LDAP FAILED.";
    
            exit;
    
        }
    
    
    // This is the stuff I can't get to work!	
    
    //	$filter="(|(uid=$username*))";
    
    //	$justthese = array("uid", "departmentnumber", "maillocaladdress"); 
    
    
    
    $search =  ldap_search($ds, "dc=mycollege,dc=ac,dc=uk", "uid=$username"/*$filter, $justthese*/);
    
          
    
    $info = ldap_get_entries($ds, $search);
    
    
    
    
    
    print_r ($search);
    
    
    
    
    ?>

     

     

  6. Hi folks,

     

    I'm taking my first steps into the world of Ajax, and my first project is to write a little script to lookup usernames from our LDAP database. The idea is that when a user starts typing usernames they get offered a lst of potential answers. Standard ajax fayre I guess!

     

    I've managed to make some headway but I could do with some help to finish it off. BTW if the code looks odd it's because this is all bastardised code from online tutorials shunted together to try and see what's going on!! :-)

     

    OK the HTML bit ...

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Photocopying</title>
    </head>
    
    <body>
    <script language="javascript" type="text/javascript">
    <!--
    
          // Get the HTTP Object
    
          function getHTTPObject(){
    
          if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
    
          else if (window.XMLHttpRequest) return new XMLHttpRequest();
    
          else {
    
          alert("Your browser does not support AJAX.");
    
          return null;
    
          }
    
          }
    
           
    
          // Change the value of the outputText field
    
          function setOutput(){
    
          if(httpObject.readyState == 4){
    
          document.getElementById('outputText').value = httpObject.responseText;
    
          }
    
    
          }
    
          // Implement business logic
    
          function doWork(){
    
          httpObject = getHTTPObject();
    
          if (httpObject != null) {
    
          httpObject.open("GET", "scripts/ldapsearch.php?inputText="
    
          +document.getElementById('inputText').value, true);
    
          httpObject.send(null);
    
          httpObject.onreadystatechange = setOutput;
    
          }
    
          }
    
    
          var httpObject = null;
          //-->
    </script>
    
    <form name="ldapsearch">
    <div id="username">Username: <input type="text" onkeyup="doWork();" name="inputText" id="inputText" /></div>
    
    <div id="">Output text: <input type="text" name="outputText" id="outputText" /></div>
    </form>
    </body>
    </html>

     

    the php bit ...

     

    <?php
    
    if (isset($_GET['inputText']))
    
    $username = trim($_GET['inputText']);
    
    // LDAP variables
    $ldaphost = "ldap.myplace.ac.uk";  // your ldap servers
    $ldapport = 389;                 // your ldap server's port number
    
    // Connecting to LDAP
    $ds = ldap_connect($ldaphost, $ldapport) or die ("Could not connect to $ldaphost");
    
    //Connection made -- bind anonymously and get dn for username.
        $bind = @ldap_bind($ds);
        
    //Check to make sure we're bound.
        if( !bind )
        {
            echo "Anonymous bind to LDAP FAILED.  Contact Tech Services! (Debug 2)";
            TabBot();
            exit;
        }
        
    $search =  ldap_search($ds, "dc=myplace,dc=ac,dc=uk", "uid=$username");
          
    $info = ldap_get_entries($ds, $search);
    
    print_r "$info";
    
    ?>

     

    The issue at the moment is that the output of the search if an array. Do I loop the output as if it came from a database and then print it out at the end of the file? Am I on the right track? Sorry if these sound like really basic questions but Ajax is two hours new at the moment!!

     

    TIA

     

    Huw

     

  7. In your case, only the user huw would have access to that .ssh directory. Either make a new key for Apache/PHP or move the key to where it is writable.

    I'll try this tomorrow.

     

     

    I wouldn't recommend using the root user though. You should use an unprivileged user unless the command you wish to run absolutely needs root.

    Yes. I'm thinking of writing some code to manage some server processes that run as root. The good new though is that the machines are not accessible from the net. Even so I'm putting security at the top of the agenda. :)

  8. Still no joy  :'(

     

    $stream = shell_exec("ssh root@itportal -i /home/huw/.ssh/id_dsa 'hostname'");
    
    echo "<pre>$stream</pre>";
    

     

    Did I miss something? If the code is OK I guess it will be a permissions issue with the .ssh directory.

  9. Note that it will try with the private key for the user that PHP is run as (which means the user the web server is run as if you run it through a web server). You might want to explicitly set the identity file when calling ssh.

     

    That sounds about right. How can I explicitly set the identity file?

     

    Thanks for your help. This is very useful :)

  10. Learn regular expressions.

     

    Read the file into PHP

    Use regular expressions to search for "Age:"

    Grab the bit afterwards

    Close the file

     

    Something like that anyway! Regular expressions is your friend - but not fun to learn :'( :'(

  11. HI folks

     

    Has anyone successfully used PHP with authorized keys to access another server?

     

    I'm playing with the idea of writing a control panel for a Linux app. I don't want to keep the root passwords in a database so authorized keys looks like a good way forward. However I can't seem to get test code to work :(

     

    <?php
    
    $test = shell_exec('ssh root@myserver; uname -a');
    
    echo "<pre>$test</pre>";
    ?>

     

    Running this *should* IMHO give me the output of uname -a on 'myserver'. It doesn't. It gives me the output of uname -a on my laptop! However running exactly the same command in my laptops terminal window gives me the correct response i.e. uname -a on myserver.

     

    Any ideas?

     

    Huw

  12. Hi folks,

     

    I'm thinking of writing some code to execute commands on a different server to the one on which LAMPS will be running. For example changing  user quotas etc. on a Samba server etc.

     

    Can anyone recommend a way of doing this? My first thought was to execute shell scripts on the localhost (with all the ssh stuff in the script) but I'm sure there are other, probably better, ways of doing the same thing.

     

    TIA

     

    Huw

  13. Are you feeling OK?

    No. I am completely fed up with seeing answers like this on 'self-help' forums.

     

    You get a link to a tutorial explaining what you should do and it makes you unhappy?

    Do you go around posting www.php.net as the answer to every PHP question? But you can get most of the answers you need by looking there ... Do you get my point?

     

    I am familiar with database design - having taught myself over a period of years from books and on-line articles. What I did this morning was a mistake - but what turned out to be an interesting one. Many to many relationships are difficult to manage and sometimes unavoidable - that is the situation I have. I either have to devise a simpler way of handling the data or introduce an extra table to handle the many to many relationship ... Hardly 'total beginner' stuff. Your reply was exactly as helpful as giving me a link to Google - no difference.

     

  14. Why did you bother Tim? What the hell was the point of that reply? Abso-f'in- useless. With cretins like you around we may as well all answer RTFM to every question and close the forum. A smart Alec reply from ... ach, what's the point? You're probably too arrogant to understand.

     

    The point of a forum like this is that people can ask questions to others - and hopefully get some sensible answers. I made a mistake with my design and could have done with a little help. Eejit.

     

    Have a nice day.

  15. Thanks for the reply Gizmola. OK so my table design seems to be flawed - can someone suggest how I should reconfigure the tables to make this work properly? Now is the time to do it before any serious data gets involved!

     

    The important thing is that users can choose three out of the ten options listed in table 'Options'.

     

     

  16. Hi folks,

     

    I'm having a bit of a 'mare this morning and would very much appreciate a little help. I have two tables: users and options.

     

    Options is a little like this:

    Value        Option

      1            Veg

      2            meat

      3            Ice cream

     

    etc etc

     

    The users table is pretty simple in data terms

     

    Username        Email        Option 1  Option 2  Option 3

    Fred              fred@            1            2          3

     

    The values of Option 1 - 3 correspond with their chosen options. I'm trying to write a query which lets me list my users and their chosen options - but with the option values replaced with the appropriate words.

     

    Simple? I'm sure it is but my brain can't figure it today. Been turning in LEFT JOIN circles for over an hour !!

  17. Interesting! It returned this

     

    login texttest did work

     

    and then all the index.php stuff. No errors.

     

    Seems like exit(); stops the execution of session.php but not index.php even though it's an  included file. Is this normal? I'm 99% sure I've used this method before without this problem.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.