klausenbusk
klausenbusk

Reputation: 41

How to connect to WIFI from a browser with Javascript and PHP?

I working on creating a Linux Digital signage Box, which my clients can buy, and choose what to show on it. At the moment, it boot up in Firefox, check if it have internet, query my server and get a unique URL which Firefox change to. If it don't have any internet connection, i show a page "You dont have internet, please connect a ethernet cable or connect to WIFI."

That my question, what will be the best way to allow my client to connect a Wireless hotspot from Firefox? At the moment my idea is:

I add a "Connect wireless" button, which with some javascript call a php script, which SSH to the BOX (localhost), and use connmanctl to first, find all WIFI hotspot and if they reguire password. The Javascript then show this, and SSH again to connect and so on..

Do you know a opensource system, which do that? or a better way to do it?

Upvotes: 3

Views: 15888

Answers (2)

sanchescom
sanchescom

Reputation: 11

I am using php-wifi in my project. It gives you interfaces to scan, connect and disconnects to wifi. Based on embedded in OS utilities. Here is an example of a controller and web form:

Connect to wi-fi with PHP - screen

<?php

use Sanchescom\WiFi\WiFi;

class Example
{
    public $device;

    /**
     * @throws Exception
     */
    public function getAllNetworks()
    {
        $allNetworks = WiFi::scan()->getAll();

        if (count($allNetworks) > 0) {
            foreach ($allNetworks as $network) {
                echo $network . "\n";
            }
        }
    }

    /**
     * @param $ssid
     * @param $password
     * @throws Exception
     */
    public function connect($ssid, $password)
    {
        $networks = WiFi::scan()
            ->getBySsid($ssid);

        if (count($networks) > 0) {
            $networks[0]->connect($password, $this->device);
        } else {
            echo "Network $ssid wasn't found!\r\n";
        }
    }

    /**
     * @throws Exception
     */
    public function disconnect()
    {
        $connectedNetworks = WiFi::scan()->getConnected();

        foreach ($connectedNetworks as $network) {
            $network->disconnect($this->device);
        }
    }
}

$example = new Example();
try {
    $example->device = 'en1';
    $example->getAllNetworks();
    $example->connect('Redmi', '12345');
    $example->disconnect();
} catch (Exception $e) {
    //
}

Upvotes: 1

Maarten Venema
Maarten Venema

Reputation: 324

This is what i would do.
You can run system code with the function shell_exec. Use this function in your PHP script to execute the native WiFi connection possibilities of your Linux distribution. How to do this for your distro is most likey answered here.
Good luck!

Upvotes: 3

Related Questions