Ok, So I’m setting up a device that has Wi-Fi and an ePaper display.
Usually, the device connects to the internet via Wi-Fi, and everything procedes according to plan. Occasionally, however, I want to take my device to a new location with a different Wi-Fi connection.
What I want to achieve is:
- The device recognises that it has lost connectivity and does a quick
scan of available Access Points in the area. - It then disables it’s client Wi-Fi settings, Enables a Wi-Fi hot-spot and displays a QR
code on the display for a smart phone to connect. - The smart phone user then selects the Wi-Fi access point from the list scanned in step 1 above (displayed in web page format).
- The user then enters the password for that Access Point.
- At this point the device attempts to connect to that AP and life goes on.
Right now, I’m having a problem getting the selected Wi-Fi connection and password saved onto the web server (step 4).
I’ve kind of realised that I need to use PHP and CGI, However, all the code that I’ve tried has not created any files on my device (Apache web server).
Most recently, I’ve tried dialing it all the way back and just hard coding what and where I want it to save, and still getting no joy:
<html>
<head>
</head>
<body>
<h1>some test</h1>
<form method="post">
Access Point Password:<br>
<input type="text" name="passwd" id="passwd" size="40"><br>
<input type="submit" name="save">
</form>
</body>
</html>
<?php
$passwd = $_POST['passwd'];
$fp = fopen('/tmp/wifi.pwd', 'a');
fwrite($fp, $passwd);
fclose($fp);
?>
My PHP version is PHP 7.3.27-1~deb10u1 (cli) (built: Feb 13 2021 16:31:40) ( NTS )
The device is running Raspbian GNU/Linux 10 (buster)
Properties on /tmp/
are drwxrwxrwt
, so anyone should be able to create files there.
One more note – I cannot use a database to accomplish this. It needs to be done as plaintext.
======UPDATE======
Ok, sorry about the delay, new job, lots of training, lots of work, very little time for family, even less for me
So I’ve been playing around (a lot) with the code. – I’ve updated the above with the code I’m currently using
I’ve also tried:
<?php
print_r($_POST);
if( !empty($_POST['passwd']) ) {
$passwd = $_POST['passwd'];
file_put_contents("/tmp/wifi.pwd", $passwd . "n", FILE_APPEND);
exit();
}
?>
Still no file creation.
2
Answers
OK, I'm a little disappointed and even a little angry that this was the answer because it's a limitation in the program itself that provides no errors, warnings or notifications.
I managed to find my answer in a buried answer to another question with NO upvotes on it. (Needless to say it's got an upvote NOW!)
I'm not even going to link to the quesiton asked there, but the answer provided that solved the problem I was having.
https://askubuntu.com/a/1138314/548774
The bottom line is that Apache2 will NOT WRITE TO ANYWHERE ON THE FILE SYSTEM OTHER THAN /var/www/ OR A SUB DIRECTORY
The errors that I was experiencing with PHP are because I introduced them trying to troubleshoot why it would not write the file to /tmp/.
Undefined index: passwd in /var/www/html/net/1.php on line 240
That means
$_POST
array exists, but the'passwd'
is not in it. Do aprint_r($_POST);
to what is posted then.Check your php.ini and make sure
post_max_size
is not 0.Note the first time you load the page,
$_POST
will be empty. So once you will see this error message for sure. The$_POST
will only contain data when you submitted the form.If you want to avoid that error message the first time, then do