Solved!..

## Get Username
	// Get ip
   		if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) { $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; } else { $ip = $_SERVER["REMOTE_ADDR"]; }
	// Send ReqPacket
		$fp = fsockopen('udp://'.$ip, 137); 
		fwrite($fp, "\x80b\0\0\0\1\0\0\0\0\0\0 CKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\0\0!\0\1");
	// Get Data (2 sec timeout)
		socket_set_timeout($fp, 2); 
		$data = fread($fp, 256);
   // Get NetBios ID
   		$nbrec = ord($data[56]); 
   // Display nebios records : the username is a record of type 3
   for($i = 0; $i < $nbrec; $i++) { 
           $offset = 18 * $i; 
      if (ord($data[72 + $offset]) == 3) 
           { 
              $userid = trim(substr($data, 57 + $offset, 15)); 
           }      
} // Put variable in place $userid = str_replace('$','',$userid);
    a year later

    Where did you place this code? In your application or within Apache or PHP?

      D8TA;11000816 wrote:

      Where did you place this code? In your application or within Apache or PHP?

      Oh wow this thread is OLD!
      I placed that in my application, it's just PHP code

        @: Given that the above is PHP code, I'm not sure how one would place it "within" Apache or PHP, so that should help narrow down the possible answers. 🙂

        Also note that a better solution (IMHO) would be to use something like mod_auth_sspi instead.

        EDIT: Didn't see (or expect!) Clarkeez's reply before posting.

          bradgrafelman;11000818 wrote:

          Also note that a much better solution (IMHO) would be to use something like mod_auth_sspi instead.

          bradgrafelman is correct - mod-auth-sspi is a better solution as my solution involved a little hacking. However, mod-auth-sspi didn't work in my environment.

            bradgrafelman;11000818 wrote:

            EDIT: Didn't see (or expect!) Clarkeez's reply before posting.

            aha, I got the email through and I was like WTF is this? aha

              Clarkeez;11000819 wrote:

              However, mod-auth-sspi didn't work in my environment.

              Out of curiosity, why is that? (Rather, in what way didn't it work in your environment?)

                bradgrafelman;11000821 wrote:

                Out of curiosity, why is that?

                This was too long ago for me to remember - I literally have no idea.

                The environment was in Santander Bank Head office actually, in the IT department.
                I was working as a IT Analyst and I knew my way around websites and php so I built our team a new portal / intranet.
                All I remember is doing alot of cussing trying to get mod-auth-sspi to work - I can't remember the exact reason.

                  bradgrafelman;11000818 wrote:

                  @: Given that the above is PHP code, I'm not sure how one would place it "within" Apache or PHP, so that should help narrow down the possible answers. 🙂

                  Also note that a better solution (IMHO) would be to use something like mod_auth_sspi instead.

                  EDIT: Didn't see (or expect!) Clarkeez's reply before posting.

                  I guess I should have mentioned this PHP application is running on SUSE Linux and not Windows which the mod_auth_sspi appears to support.

                    True, I was assuming Windows was the target OS since that was true in the thread that you've hijacked. 😉

                    If you're on Linux, you might need to use something like mod_auth_ntlm_winbind as well as setting up a winbindd instance.

                      9 years later

                      Hi Clarkeez,
                      I was looking for a way to get windows username from a php script running on linux server. I have tested your solution it works!
                      but in $data there are two records with type=3 the first is the username, the second is the device name, is there a way to identificate who is who?
                      Thanks

                      uffa14 As this is a 10-year-old thread, chances are that poster may no longer be active here.

                      You may want to consider opening a new thread with the applicable code you are using and the specifics of what you need changed/fixed.

                        Write a Reply...