-4

This is the entire code. The problem is at the last line. I didn't write it, I've copied it from my wireless hotspot application forum and I have this error. Can someone give me a hint on how to solve it? Thanks

<?php
$ipaddress=$_SERVER['REMOTE_ADDR'];
$macaddr=false;

#run the external command, break output into lines

$arp=`arp $ipaddress`;

$lines = explode(" ", $arp);

$macaddr = $lines[3];


?>
Lmest
  • 3
  • 2
  • 2
    wow ... if you seriously think that we are code-mill you are in the wrong neighbourhood .... Do it your self and ask if you have problems .. – Mingebag Apr 11 '14 at 08:02

2 Answers2

0

What do you need $macaddr to contain?

<?php
//RETURNS AN IP ADRES 127.0.0.1
$ipaddress=$_SERVER['REMOTE_ADDR'];

$macaddr = false;

#run the external command, break output into lines

$arp='arp ' . $ipaddress;

//You're trying to explode `arp 127.0.0.1` with a space
$lines = explode(" ", $arp);

//$lines contains array(0 => 'arp', 1 => '127.0.0.1') 

//$lines[3] does not exist that's where the undefined offset is coming from.
$macaddr = $lines[3];


?>
Daan
  • 12,099
  • 6
  • 34
  • 51
0

So let's walk through exactly what this code is doing.

First, we're getting the IP address of the user that is currently viewing the page. $_SERVER is a predefined variable available on all* boxes running php. The documentation shows this here:

The IP address from which the user is viewing the current page.

Then you're setting a variable $macaddr to false. You'll be filling this with data, but it might still be false in the future, so you should be performing some checks before attempting to access it. More on that later...

PHP has an execution operator. This involves wrapping code in backticks to execute code on the server and is pretty much the same as shell_exec() - except when using it you don't have the overhead of a function call.

You're executing arp 127.0.0.1 as you would in the command line and returning the results as an array before placing it in the $arp variable.

You're then splitting the string with explode() on any spaces, and finally attempting to read the third element of the array.

Firstly, you should be checking that the third element of the array exists. If it doesn't, the arp command went wrong and didn't return what you expected, and you should be throwing useful Exceptions to explain what went wrong to future users of your code.

Secondly, you should be checking at the end that $macaddr contains the sort of information you want, possibly by a regex or similar validation functionality.

Finally, why aren't you getting back what you want from sending arp to the command line? Well, first, try running arp yourIpAddressHere in your command line and make sure it's returning some data. I get "no entry" when I do it, so sort out your command first, in command-line, before getting PHP to do it so you can use it in your code.

*Unless you're been messing around with things you shouldn't have been.


Why I think it won't work

I'm trying your arp command. Here are my results:

  • Trying arp on localhost with my ip address, both internal and external

I get "no entry" back. Because on my localhost the addresses are all hostnames and not ip addresses, and mine isn't in the list.

  • Trying arp on a remote server

I only get back the ip address of the server from the arp command. So trying to access the visiting user's mac address just won't work, because the execution is happening on the server. Not on the user's computer.

In conclusion, you're trying to do something hacky - accessing the user's mac address for nefarious purposes. I'm not about to help you any further with this answer.

Jimbo
  • 25,790
  • 15
  • 86
  • 131
  • I am an internet provider and I have no knowledge of php. I give wi-fi for free for about 1 hour all over the town where I've installed the hotspots. And I need the mac address to set restrictions. I've just copied and pasted the code like I've said, from the official forum of the hardware I've bought. – Lmest Apr 11 '14 at 08:38
  • Are you aware the mac address can easily be changed or spoofed by a user with little knowledge and a simple copied and pasted line of code from the internet? I think your question might be "how to uniquely identify a user on a computer with php" - you might find [resources](http://stackoverflow.com/questions/5048066/how-to-uniquely-identify-a-computer) to help you do this if you search google / stackoverflow on this subject. I wouldn't rely on a mac address, but a combination of things like IP, cookies, SESSION etc. – Jimbo Apr 11 '14 at 08:45
  • I just found [evercookie](http://samy.pl/evercookie/) which looks very interesting. Check that out, it might create the persistence you need. – Jimbo Apr 11 '14 at 08:52