LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-04-2005, 04:50 AM   #1
Erhnam
Member
 
Registered: Jan 2005
Posts: 54

Rep: Reputation: 15
[php] display the usernames of autharray


On my local linux machine I have a extracted with a perl script from the /etc/passwd file a list of usernames. In php I want to display all usernames, and later on manipulate them. Some example data:

infobot:!!:510:511::/home/infobot:/bin/bash
erhnam:$1$xluT7i6/$fEqbxpnBPjM:513:514::/home/erhnam:/bin/bash
mhuser01:$1$j.u3BOFHmfoo2Ab/:514:515::/home/mohaa/:/bin/bash

I tried to use the explode function to extract only the username. But I'm only getting back: 'array'. Code:

Code:
$autharray = file("/var/www/html/passwd.file");

$userlist = explode(":", $autharray);
$overview = $userlist[0];

echo $userlist;
echo ("<br>");
echo $overview;
echo ("<br>");
echo nl2br(implode("\n",$autharray));
echo ("<br>");
?>
I'm able to display all the values with: echo nl2br(implode("\n",$autharray));

echo $userlist; gives me Array
echo $overview; gives me also Array

Anyone can tell me how I can display only the usernames on the screen?
 
Old 02-04-2005, 05:24 AM   #2
N43
LQ Newbie
 
Registered: Sep 2004
Location: Germany
Distribution: Slackware
Posts: 13

Rep: Reputation: 0
file() returns an array. You have to cyle threw the array with foreach.

PHP Code:
<?php
   $autharray 
file("/var/www/html/passwd.file");

   foreach (
$autharray as $auth)
   {
      
$userlist[] = explode(":"$auth);
   }

   
$overview $userlist[0][0];
   echo 
$userlist;
   echo (
"<br>");
   echo 
$overview;
   echo (
"<br>");
   echo 
nl2br(implode("\n",$autharray));
   echo (
"<br>");
?>
echo $userlist will still display Array but echo $overview will now display a user name.

I didn't test the code but it should work.
 
Old 02-04-2005, 05:42 AM   #3
Erhnam
Member
 
Registered: Jan 2005
Posts: 54

Original Poster
Rep: Reputation: 15
Thanks, but I'm now only getting the first value from the Array. Do you what to do, to get the whole list?

Code:
for ($x = 0; $x < count($autharray); $x++)
But how to implement this??

Last edited by Erhnam; 02-04-2005 at 06:18 AM.
 
Old 02-04-2005, 07:40 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
I would avoid the use of file() because it is a slow function, instead use fopen() or
file_get_contents().
If all you want is the usernames from the /etc/passwd file, you could do :
PHP Code:
$fd fopen("/etc/passwd""r");
$users = Array();
while ((
$line fgetcsv($fd1000":")) !== FALSE) {
    
$users[] = $line[0];
}
fclose($fd);
for (
$x 0$count count($users); $x $count$x++) {
    echo 
$users[$x]."<br>";


Last edited by keefaz; 02-04-2005 at 07:42 AM.
 
Old 02-04-2005, 07:58 AM   #5
Erhnam
Member
 
Registered: Jan 2005
Posts: 54

Original Poster
Rep: Reputation: 15
Thanks a lot!!

Do you know if it would be possible to search.. I already tried some thing like this.. But it is not working for me

$search = Array();
$users = array_search ("001", $search);

Last edited by Erhnam; 02-04-2005 at 08:01 AM.
 
Old 02-04-2005, 08:08 AM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
search the position for user in the list ?
PHP Code:
$key array_search("root"$users);
if(
$key === FALSE) {
    echo 
"Weird, no root user...";
    exit(
1);
} else {
    echo 
"found root at position $key of the users array";

 
Old 02-04-2005, 08:12 AM   #7
Erhnam
Member
 
Registered: Jan 2005
Posts: 54

Original Poster
Rep: Reputation: 15
This is not realy what I mean.. I want to list all the user that has (for example) a value in it with 'ab'

So if the array is (dog, monkey, abba, abo, mouse)
The result should be:

abba
abo
 
Old 02-04-2005, 08:29 AM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
I have not idea of an elegant solution, I would do :

PHP Code:
$result = Array();
$search "ab";
for (
$x 0$count count($users); $x $count$x++) {
    if((
$found strripos($users[$x], $search)) !== FALSE) {
        
$result[] = $users[$x];
    }
}

if(
count($result) > 0) {
    echo 
"user(s) found !";
    
print_r($result);
} else {
    echo 
"no user was found";

[edit]
strripos seems to be in PHP5 only...
So add these lines for PHP4 (example from php.net) :
PHP Code:
function strripos($haystack$needle)
{
   return 
strpos(strtolower($haystack), strtolower($needle));


Last edited by keefaz; 02-04-2005 at 08:34 AM.
 
Old 02-08-2005, 08:31 AM   #9
Erhnam
Member
 
Registered: Jan 2005
Posts: 54

Original Poster
Rep: Reputation: 15
Okey! This worked for me. I used the print and echo functions at the end of the screen to see if it works and yes! I'm now getting a nice filtered list.. My last problem:

Now I have a list with all the users I want to combine it together with the command below. For each user I want to see the results of 'netstat -ea | grep user'.

Like:

user01:
tcp 0 0 83.98.xx.xx:xxxx *:* LISTEN user01 1141153923

Do you think this is possible? And where to start!? Really thanks for all your help! I tried already something with:


PHP Code:
foreach ($result as $inhoud) {
$inhoud $inhoud;
echo 
"$inhoud<br /> \"'netstat -ea | grep $inhoud'\"<br /><br />\n";
}
echo 
"<br><br>"
or

PHP Code:
foreach ($result as $inhoud) {
$inhoud $inhoud;
echo 
"$inhoud<br />\n";
exec ("'netstat -ea | grep $inhoud'"$userstatus);
echo 
nl2br(implode("\n",$userstatus));
print (
"<br>"); 
PHP Code:
<?

function strripos($haystack$needle)
{
   return 
strpos(strtolower($haystack), strtolower($needle));
}

$fd fopen("/var/passwd""r"); 

$users = Array(); 
while ((
$line fgetcsv($fd1000":")) !== FALSE) { 
    
$users[] = $line[0]; 

fclose($fd); 

$result = Array();
$search "user";
for (
$x 0$count count($users); $x $count$x++) {
    if((
$found strripos($users[$x], $search)) !== FALSE) {
        
$result[] = $users[$x];
    }
}


if(
count($result) > 0) {
    echo 
"user(s) found !<br><br>";

    echo 
"$result<br><br>";

    
print_r ($result);
    echo 
"<br><br>";

for (
$y 0$count count($result); $y $count$y++) {
echo 
$result[$y]."<br>";

}

} else {
    echo 
"no user was found";
}

?>

Last edited by Erhnam; 02-08-2005 at 09:39 AM.
 
Old 02-08-2005, 12:50 PM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
I would use :
PHP Code:
...
...
// find if a string contain an userfrom the list, if yes return its position
function find_user($list$string) {
    
$i 0;
    foreach(
$list as $user) {
        if(
strriipos($string$user) > 0) {
            return 
$i;
        }
        
$i++;
    }
    return 
false;
}

// Array where all relevant netstat lines will be stored, its key will be
// the user name
$users_net = Array();

// open a pipe so you can easilly read line by line
$netstat popen("netstat -an""r");

if(!
$netstat) {
    echo 
"could not open netstat");
    exit;
}

while(
$line fgets($netstat1024)) {
    
$key find_user($users$line);
    if(
$key !== false) {
        
$users_net[$users[$key]][] = $line;
    }
}

pclose($netstat);

// output result
print_r($users_net); 
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
firefox cant display php Xris718 Linux - General 3 11-01-2005 10:02 AM
PHP - imagettftext doesn't display text Adrohak Linux - Software 0 09-18-2004 03:24 PM
php won't display errors Red Squirrel Linux - Software 4 03-14-2004 01:28 PM
Galeon won't display PHP pages... VIP3R Linux - Software 1 12-29-2003 10:48 AM
Usernames Zwitterion Linux - General 1 12-25-2002 08:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration