We create a connection to the database.
Not a single site is complete without navigation, or as the "site menu" is also called. So the site menu can be single-level and multi-level in the form of a tree. If there are no particular difficulties in terms of implementation with a single-level menu, then when creating multilevel menu need to think well.
The most important thing in this task is to design a database for our multi-level menu. Let's create a Categories table with three fields id, title, parent where:
- ID- identifier
- Title- Menu name
- parent- Default category parent 0
The field is responsible for branching the menu parent if parent = 0, then this category is the parent category. In order to add children to the parent category, you need to specify in the parent field ID the desired parent. For example:
Tables with categories
As can be seen from the table, the parent category Cars there are two descendants Mazda and Honda related by field parent. And the category Motorcycles two offspring are kawasaki and harley. At the same time, the category Boats has no descendants. I hope you understand how to link categories.
Next, we move from words to practice. Let's create the Categories table.
CREATE TABLE IF NOT EXISTS `categories` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `parent` int(10) unsigned NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=20 ; -- -- Dump `categories` table data -- INSERT INTO `categories` (`id`, `title`, `parent`) VALUES (1, "Cars", 0), (2, "Motorcycles", 0) , (3, Mazda, 1), (4, Honda, 1), (5, Kawasaki, 2), (6, Harley, 2), (7, Mazda 3, 3 ), (8, "Mazda 6", 3), (9, "Sedan", 7), (10, "Hatchback", 7), (11, "Boats", 0), (12, "Liftback", 8), (13, "Crossover", 8), (14, "White", 13), (15, "Red", 13), (16, "Black", 13), (17, "Green", 13), (18, Mazda CX, 3), (19, Mazda MX, 3);The algorithm of work consists of the following:
Creating a database connection
query("SET NAMES "utf8""); /* * This is the "official" object-oriented way to do this * however $connect_error didn't work until PHP 5.2.9 and 5.3.0. */ if ($mysqli->connect_error) ( die("Connection failed (" . $mysqli->connect_errno . ") " . $mysqli->connect_error); ) /* * If you want to be sure about compatibility with versions prior to 5.2 .9, * better code like this */ if (mysqli_connect_error()) ( die("Connection failed (" . mysqli_connect_errno() . ") " . mysqli_connect_error()); )Writing a function to get data from the Categories table
//Get our menu array from the database as an array function getCat($mysqli)( $sql = "SELECT * FROM `categories`"; $res = $mysqli->query($sql); //Create an array where the array key is the menu ID $cat = array(); while($row = $res->fetch_assoc())( $cat[$row["id"]] = $row; ) return $cat; )We get an array like this, where the array key is the category ID.
Array Tree Function by Tommy Lacroix
//The function to build a tree from an array from Tommy Lacroix function getTree($dataset) ( $tree = array(); foreach ($dataset as $id => &$node) ( //If there are no attachments if (!$node[" parent"])( $tree[$id] = &$node; )else( //If there are children, then loop through the array $dataset[$node["parent"]]["childs"][$id] = &$ node; ) ) return $tree; )Getting an array in the form of a tree
Whole script
query("SET NAMES "utf8""); /* * This is the "official" object-oriented way to do this * however $connect_error didn't work until PHP 5.2.9 and 5.3.0. */ if ($mysqli->connect_error) ( die("Connection failed (" . $mysqli->connect_errno . ") " . $mysqli->connect_error); ) /* * If you want to be sure about compatibility with versions prior to 5.2 .9, * it's better to use this code */ if (mysqli_connect_error()) ( die("Connection error (" . mysqli_connect_errno() . ") " . mysqli_connect_error()); ) //Get our menu array from the database as an array function getCat($mysqli)( $sql = "SELECT * FROM `categories`"; $res = $mysqli->query($sql); //Create an array where the array key is the menu ID $cat = array(); while ($row = $res->fetch_assoc())( $cat[$row["id"]] = $row; ) return $cat; ) //Function to build a tree from an array by Tommy Lacroix function getTree($dataset) ( $tree = array(); foreach ($dataset as $id => &$node) ( //If there are no attachments if (!$node["parent"])( $tree[$id] = &$node; )else( //If there are children, then iterate through the array $dataset[$node["parent"]]["childs"][$id] = &$node; ) ) return $tree; ) //Get prepared th array with data $cat = getCat($mysqli); //Create a tree menu $tree = getTree($cat); //Template for displaying the menu in the form of a tree function tplMenu($category)( $menu = "- ".showCat($category["children"]) ."
- ". $cat_menu ."
The result of the work
Multilevel menu in PHP + MySQL for admin
If you want to use this menu in the admin panel of your site, you need to rewrite a couple of functions tplMenu(), showCat().
".$category["title"].""; )else( $menu = " "; ) if(isset($category["childs"]))( $i = 1; for($j = 0; $j< $i; $j++){ $str .= "→"; } $i++; $menu .= showCat($category["childs"], $str); } return $menu; } /** * Рекурсивно считываем наш шаблон **/ function showCat($data, $str){ $string = ""; $str = $str; foreach($data as $item){ $string .= tplMenu($item, $str); } return $string; } //Получаем HTML разметку $cat_menu = showCat($tree, ""); //Выводим на экран echo ""; ?>
The result of the work
Select Cars → Mazda →→ Mazda 3 →→→ Sedan →→→ Hatchback →→ Mazda 6 →→→ Liftback →→→ Crossover →→→→ White →→→→ Red →→→→ Black →→→→ Green →→ Mazda CX →→ Mazda MX → Honda Motorcycles → Kawasaki → Harley Boats1C-Bitrix - highly professional administration system Your web projects, which allows you to flexibly manage online store orders, which reduces the volume of repetitive processes and improve the quality of customer service.
In addition to the main advantages of the Bitrix system, all users the high demands of this product are known to the technical parameters of the hosting. We managed to optimize and tune our servers. Now HyperHost is an excellent, high performance host.
Below we will consider the process of installing this system on our hosting:
step 1
Before starting the installation of CMS Bitrix, you need to make some changes to the individual php.ini for your account. How to do this, see the screenshots:
step 2
After entering the necessary parameters in the individual php.ini for Bitrix CMS, you need to download and upload the installation script:
Bitrix CMS (bitrixsetup.php).
You can download it at the address from the official website 1c-bitrix.ru:
http://www.1c-bitrix.ru/download/cms.php
step 3
Next, we need to upload it to the server in the home directory of your site.
This can be done both through the Cpanel control panel and using FTP
To download through the control panel, you need to do the following:
step 4
To install the site, we need to create a database (DB), a database user and add the user to the created database
step 5
Let's start installing CMS Bitrix
Important! You can check the operation of the site on our hosting locally, without changing the DNS (domain delegation), you just need to register in hosts file following entry:
53.114.112.45 domain.ru
where,
domain.ru - your domain.
53.114.112.45 - ip-address of your hosting server. To see what IP your site is located in our company after ordering, you need to do the following:
Go to the site control panel in the "Web domains" section and see what IP address is specified for your site;
The hosts file is located in the following paths:
- Windows XP/2003/Vista/7: WINDOWS\system32\drivers\etc\hosts
- Windows NT/2000: WINNT\system32\drivers\etc\hosts
- Windows 95/98/ME: WINDOWS\hosts
- Mac OS X 10.2+: /private/etc/hosts
- Linux: /etc/hosts
After making these changes, the version of the site that you transferred to our hosting will open at the domain.ru address from your computer browser.
Don't forget to remove this entry from the hosts file after checking your site's performance.
Navigate in your browser to http://your_domain/bitrixsetup.php, select the distribution kit you will install.
We will show you an example of installing a CMS on the “Site Management” distribution kit and a demo version
The next step is to download the distribution to the server and unpack it.
step 6
CMS installation
After unpacking the distribution, you will be redirected to the CMS installation page
Read and confirm the license agreement
step 7
We enter our data and put a “tick” next to the item “Install in UTF-8 encoding”, after that we proceed to the next step
step 8
In this step, the verification page is displayed, here it is checked for compliance with the server settings for correct installation and work of CMS Bitrix.
step 9
Establishing a connection to the database.
In the fields you need to specify the connection data to the database that we created in step 4
step 10
This step goes automatic installation product
step 11
Create an administrator for this site
You need to fill in the required fields: login, password and password confirmation.
step 12
Select an installation template.
On the this moment settings, it is only 1 - “fixed”, since this is the standard setting.
Topic selection
Enter information about your company for which you are installing the site and start the installation of the selected theme.
Congratulations! You have installed CMS Bitrix.
It should be noted that the test results showed very high 1C performance results on our servers.
On the VPS-Start tariff from our line, the performance was 88,58
at the standard 30
showed better results 127,84
with the standard 30
These figures are among the highest among hosting companies in the CIS.
HyperHost company wishes you a pleasant work!
8775 time(s) 9 times viewed today
With this programming language, you can create websites and Web applications in local computer via Apache server. Most modern Internet sites are written in PHP. The pages of these sites are dynamic, unlike the old sites on HTML markup.
All information is stored in the database MySQL data, from which the currently needed one is retrieved. Experienced programmers can create websites from scratch, and for ordinary users it is better to use the services of ready-made engines (WordPress, Joomla or Drupal), where it is enough to add your own inscriptions - and your site is ready. To create your Web sites on your local computer, you need to download for free new version PHP. After that, the finished site can be uploaded to the Internet.
Capabilities:
- creation and editing of Web-sites and programs;
- dynamic site content management;
- testing various engines for Web sites;
- use of HTTP cookies.
Principle of operation:
Now let's understand how PHP works. First you need to download and install, for example, a virtual Apache server. To do this, you can use ready-made developer kits Denwer, XAMPP, OpenServer or any other program. After installation local server you need to copy the contents of the PHP archive to it. After that, you will be able to write sites from scratch on your virtual hosting or install the necessary site engines to create your Internet projects.
Having paid for hosting and a domain, you can upload your site to the Internet and start realizing your creative abilities. Usage PHP language will allow you to quickly and conveniently create and edit the content of your sites. For example, to change the design of the bottom of the site, you only need to edit one file, and the changes will be reflected on every page of your project. In HTML, you would have to edit each page individually.
Pros:
- creation of dynamic sites in PHP;
- use of databases;
- the ability to download PHP for free on a computer
- support for creating multiple databases and sites on the local computer;
- widespread and popular programming language.
Minuses:
- to work on a computer, you may need to install a virtual server.
Today, PHP is the most common programming language for websites and Web applications. Therefore, to create your own projects, you simply need to adapt your Windows system to this language. Also, for work, you will need to install a server for virtual hosting your local sites. Using this programming language will allow you to create and run your Internet projects, so we advise you to download it for free latest version PHP.
PHP (English PHP: Hypertext Preprocessor - "PHP: Hypertext Preprocessor"; originally Personal Home Page Tools - "Tools for creating personal web pages") - scripting language general purpose, which is heavily used for web application development.
At present PHP time supported by the vast majority of hosting providers and is one of the leading languages used to create dynamic websites.
The PHP language and its interpreter are being developed by a group of enthusiasts as part of a project with open source. The project is distributed under its own license, which is incompatible with the GNU General Public License.
Scope of PHP
In the field of web programming, in particular the server side, PHP is one of the popular scripting languages (along with JSP, Perl and the languages used in ASP.NET).
Popularity in the field of building websites is determined by the presence of a large set of built-in tools for developing web applications. The main ones are:
- automatic extraction of POST and GET parameters, and environment variables web servers into predefined arrays;
- interaction large quantity various database management systems (, MySQLi, Oracle (OCI8), Oracle, Microsoft SQL Server, Sybase , ODBC, mSQL, IBM DB2, Cloudscape and Apache Derby, Informix, Ovrimos SQL, IBM Notes, DB++, DBM, dBase, DBX, FrontBase, FilePro, Ingres II, SESAM, / InterBase, Paradox File Access, MaxDB, Interface PDO (PHP Data Objects));
- automated sending of HTTP headers;
- work with HTTP authorization;
- work with cookies and sessions;
- work with local and deleted files, sockets;
- processing files uploaded to the server;
- working with XForms.
PHP is currently used by hundreds of thousands of developers. According to TIOBE Corporation ranking based on data search engines, in September 2015, PHP was in 6th place among programming languages. The largest sites using PHP include
I talked more about how to set up Denver and make sure everything works, just taking a little time to implement a simple ws echo server. But if you have downloaded the source archives, then I'm sure you can easily figure out how to receive and send messages, which is quite enough to use the technology.
I am glad that there was a reaction in my blog, which has not yet been promoted, and this prompted me to write a continuation and reveal the question of how to still make the ws server PHP script work on the hosting, how to make sure that the process is running and the PHP script is running and was not closed by timeout or when Apache was restarted, how to avoid running a duplicate PHP process and answers to other necessary questions in order to get a guaranteed working ws server. I will definitely create an Internet ws echo server with a chat function and place it on my mediocre hosting. For the remainder of this article, the PHP script that runs continuously on the server and provides ws I will refer to as the ws server, although it is actually a server-side process. And most importantly, I will give a ready-made solution even for those who do not have SSH access to the console of the hosting server or virtual machine.
There will be a lot of work today
Let's start simple.
Placing and running a web socket server on a hosting
The simplicity lies in the fact that there are no problems with the launch. You can take the PHP script echows.php from the last article, without changing anything in it, upload it to the hosting and access the file from the browser, the only difference from running in Denver may be that the Apache settings and hosting caching mechanisms prohibit displaying incomplete information PHP script, in which case you will not see any information in the browser window, and the page will continue to load in the browser. But if you try to connect to the ws server from our ws client, you will see that the connection was successful and the ws echo server responds to all transmitted requests. As the address of the ws server in the ws client, of course, you need to specify ws://yourdomain.com:8889. As you can see, everything is in order. The only problem you may encounter is the hosting firewall settings and ports being busy with other services. If you have several domains on one IP address, then you can access the ws server at the address of this domain ws://anotheromain.com:8889 which, you see, is not very good, especially for hosting where hundreds of websites hang on one domain, theoretically, someone can use the same ports as you, and this can lead to the inoperability of the ws server, so once again I strongly recommend that you carefully select ports and make sure that when ws stops working, the server always closes all connections and correctly closes sockets. Now, as for the PHP script itself, as you may have noticed, our PHP script only lives for 100 seconds, after which, when it tries to connect or send a message, it closes all connections and ends its execution. How to make it live "forever"? There are a sufficient number of methods. But the problem is not that the PHP script should run indefinitely, but the problem is how to ensure that the script ends correctly in various situations and then correctly resumes its work. For example, when the server is turned off and then enabling PHP the script itself will not start, and it is a very bad idea to monitor whether the ws server is running and if it does not work manually to start it.
PHP script infinite execution method run from browser
The first thing to do is to specify in the script itself the unlimited lifetime of the PHP script set_time_limit(0); and ignoring browser abort ignore_user_abort(true); so that the PHP script is executed after you close the browser window. When you have performed the necessary actions, you can launch it from the browser. The problem is that by running it once, we will not be able to check whether everything is in order with the process, whether there were any errors during execution, and whether the script continues to work or not can only be verified by connecting to it with the ws client. Of course, you can add the functionality of maintaining a log file to this script, in which the history of its work will be recorded. Also, when you restart Apache on the hosting, the process with the script will be 100% turned off. This method can only work if we upload a debugged ws that is guaranteed to work on the server PHP server script and it is important for us that it works only for a short foreseeable time, depending on the reliability of the hosting provider, but this method is completely inapplicable for the operation of a full-fledged industrial ws server due to its extreme unreliability and the impossibility of shutting down at the moment when we need it. Imagine a situation where the administrator of the hosting server decided to restart Apache for the purpose of updating, and you don’t check whether your ws server is constantly working or not, while users, let’s say, are sitting in your chat, and suddenly everything goes down, users are indignant. Again, you can use a couple of crutches before the ws client connects to the ws server via the ws protocol, make the ws client access another PHP server script via XMLHttpRequest(), and require the ws server to be checked for running. The method is a little crooked, but it has a place in non-industrial solutions like a small chat room or a small toy. This is what I use in my small projects.
Just in case, I conducted an experiment, launched the ws server on the hosting, having previously introduced into the PHP script a mechanism for closing all connections and interrupting the process when receiving an “OFF” message from the client. I didn’t touch it for several days, periodically sending different messages and checking survivability, sent the “OFF” command after about two days ws successfully completed its work, the ws server process lifetime turned out to be 183,403 seconds (a little over 2 days) and I think that it could work even longer without any problems.
PHP infinite execution method run from console
In principle, there is practically no difference with running a PHP script from a browser, except that all data about PHP work scripts are output to the console and a few other small nuances. The PHP script itself must also contain set_time_limit(0); and ignore_user_abort(true);. Running a PHP script from the *nix console is done with the command $ php -q scriptfile.php(I used Putty to connect to the console). The -q (--no-header) switch indicates that the process should be run in silent mode and suppresses the output of HTTP headers that are normally sent to the browser. $manphp will allow you to see other keys of interest. Excellent documentation on the official PHP website, which helped not only to deal with the launch of PHP scripts from the console, but also made significant improvements to the PHP script itself. Please note that you can redirect the output of the script to any file on the server using the 'greater than' symbol ('>'), as I usually do.
$ php -q scriptfile.php > scriptfileoutput.txtPHP can be used to run PHP scripts in absolute independence from Apache, but I am not sure that the socket mechanism will work without Apache, I did not try to run it without Apache - it seemed to me nothing. Running through the console is considered more correct than through a web browser, but it, like running through a browser, is not able to solve a number of problems. Perhaps such a launch will save us from terminating the script when restarting Apache, and this is unlikely, but what if the entire web server or virtual machine will be reloaded. You will have to manually climb onto the server and run the script, of course, if you have a large game project and have dedicated system administrators which monitor the state of processes on the server and there are scripts for initializing and loading the ws server along with Apache and everything else, in which case this is the only correct option, but we are talking about a household convenient way to implement a ws server in PHP for small projects. Also sometimes there is a problem when running a PHP script from the console, which stops the execution of the PHP script at the same time when you exit the console, this is due to the fact that the execution of the PHP script was tied to your session as a client. In theory, this should be cured by using the ignore_user_abort(true); line in the PHP script, but this does not always help due to different PHP settings. In such a case, the trick is to give the PHP script a /dev/null stream that it will assume is a client and not terminate when you exit the console.
$ php -q scriptfile.php scriptfileoutput.txt &The ampersand at the end is required so that you can press Ctrl+C and return to the console while the process remains in memory. Or you can use the nohup utility.
In addition, it will be useful to know that on the windows platform, you can run the script from the console
> w:\usr\local\php5\php.exe -q w:\home\localhost\www\echows.phpIf everything is done correctly, then it is better to use the Supervisor utility: A Process Control System, it monitors the operation of the process, starts it if necessary, and registers crashes. A great right thing when you are doing a serious project and you have a dedicated server or at least VDS at your disposal.
ws server PHP process control from browser
As a result of studying ways to launch PHP scripts, I had a very simple idea - to implement on the ws client side, before connecting to the ws server, an XMLHttpRequest () request to a PHP script that checks the status of the ws server process and, if it is not running, launches. There was also an idea to implement something like the ws server administrator page, on which a log of recent events will be available: when it was launched, why it crashed, who initiated the next launch, etc., from which it will be possible to give the ws server command to restart, turn off, close all connections, etc.
What should be the interface for the ws client:
- Check ws server status and start if server is unavailable
What should be the interface for ws administrator:
- Information about the status of the ws server, preferably with the number of connections and the amount of memory occupied
- Viewing logs
- Stopping the ws server
So, for implementation, we need a ws server, which must be hung in memory in one of the ways described earlier, i.e. you need to make a quality demon out of him. There is an excellent article on processes and daemons on Habré. However, unfortunately, finding hosting with support for the pcntl_fork command to create child processes in PHP is even more difficult than with socket support, so you have to abandon the classic daemonization method. Also, such programs cannot be debugged on windows. forks only exist on *nix operating systems. But still, we managed to learn something useful from the article, namely the creation of a PID file that stores the process id, which will not allow two processes to run simultaneously - more on this below.
As a result, I slightly modified the PHP code of the ws echo server script by accommodating the code for switching input / output streams STDIN, STDOUT, STDERR into it and, thereby, simplified the launch of the ws server from the console:
$ php -q /home/path/echows.php &Got a well working daemon without using pcntl_fork. It is launched via the console, unbinds from the console, and everything works fine to respond to all user requests at ws://yourdomain.com:8889, and closes correctly when an "OFF" message is sent. But here's the problem, it is not possible to check whether the daemon is running or not and thereby avoid a duplicate launch of the daemon. Yes, at startup, the pid_file.pid file is created, which stores the process id (unique process number in the OS * nix system) of our daemon, and when the daemon ends correctly, for example, when the “OFF” message is received, this file is deleted. At startup, you can, of course, check for the presence of this file, and if the file exists, then report that the daemon is already running and thus avoid a duplicate launch, but what if the daemon completed its work incorrectly and did not delete the pid_file.pid file, in this case our the daemon will never run again. Again, on Habré, we managed to find an excellent function for checking the presence of a daemon.
if(is_file($pidfile)) (
// check for process, posix_kill may be deprecated, may need to be replaced when migrating to PHP 5.3+
if(posix_kill($pid,0)) (
//daemon is already running
return true;
) else (
if(!unlink($pidfile)) (
consolemsg("ERROR");
exit(-1);
}
consolemsg("OK");
}
}
return false;
}
And again, the problem is that the posix_kill($pid,0) function turned out to be inoperative for the same reason as pcntl_fork. I could not put up with this and again developed a "cunning" solution. Because I still decided to implement the functionality showing the state of the ws server, then I somehow need a function that shows the status of the process in the OS. To implement this function, we will use the exec() command, which allows you to execute any console commands. And if we do
Exec("ps -aux -p ".$pid, $output);
then as a result, in the $output array, if the daemon is running and has $pid, information about the daemon (process) will be displayed.
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND aow 62335 0.3 0.1 120080 16020 ?? SJ 4:31PM 0:00.02 php -q echows.php
Thus, we got really working functions without posix_kill, which check whether the daemon is running and give out data about it, respectively.
Upd 2017.08.14: The example above uses the BSD syntax for the ps command. For most *nix systems it will be fine, and this command will list all processes instead of the one with pid. Starting with ws server admin panel v.0.4. and above uses the classic syntax, which works correctly on most *nix systems.
Function isDaemonActive($pidfile) (
if(is_file($pidfile)) (
$pid = file_get_contents($pidfile);
// get process status
$status = getDaemonStatus($pid);
if($status["run"]) (
//daemon is already running
consolemsg("daemon already running info=".$status["info"]);
return true;
) else (
//pid-file exists, but there is no process
consolemsg("there is no process with PID = ".$pid.", last termination was abnormal...");
consolemsg("try to unlink PID file...");
if(!unlink($pidfile)) (
consolemsg("ERROR");
//can't destroy pid file. error
exit(-1);
}
consolemsg("OK");
}
}
return false;
}
getDaemonStatus($pid) (
$result = array("run"=>false);
$output = null;
exec("ps -aux -p ".$pid, $output);
If(count($output)>1)(//If there is more than one line as a result of execution, then there is a process! because the first line is the header, and the second is already the process
$result["run"] = true;
$result["info"] = $output;//string with information about the process
}
return $result;
}
Now that the status check function is ready, we can start the daemon not from the console, but by executing the PHP command exec("php -q echows.php &"); and turn off the daemon with the OFF message.
The last thing to change in the ws echo server and ws client is to add an AJAX request to the server before connecting, which would raise it if it is down. Now we do not need to think about the state of the demon, because. we know that even if the server was rebooted, the client will pick it up at the first call.
Browser control panel ws server
I will develop simple system daemon control and monitoring. It is very simple and consists of several files echowsadmin.html (admin panel), echowsadmin.js (admin panel logic), echowsadmin.php (ws echo server control logic). It turned out to be surprisingly easy to develop this system, I spent no more than 1 hour of my time.
In order to prevent any user from turning off the daemon with the OFF command, I removed this functionality from the PHP code of the ws echo server. Accordingly, by implementing the shutdown function from the daemon control system. The implementation is not the most elegant, I use the off_file.pid file instead of signals, but it is guaranteed that no additional libraries are required and the shutdown occurs correctly. Because on ws server while loop hangs at the moments of listening to socket messages, then after creating off_file.pid, you need to connect to the ws server so that it reaches the end of the cycle and checks for off_file.pid, for this I do a little trick, imitate a socket connection from echowsadmin.php and introduce some delay to everything worked and the script reported a successful completion of the work. Download (outdated archive, newer in ), do not forget to specify the location of the echowsadmin.php file on your hosting in echowsadmin.js on line 10, the address of echowsstart.php in socket.js and the address of the ws server in echowsadmin.php (or rather, not the address , and the port, since the file must be on the server, the address will always be 127.0.0.1), where our single-cell mini client will try to connect when the ws echo server is turned off. I did not make the ws echo server restart button, since it is clear that for this you need to press stop, and then start, and there is practically no need for this action in one button. This entire control system works only under *nix operating systems, i.e. on hosting. And this is how she looks.
Obviously there is a lot to be improved:
- Improve the presentation and make more detailed information about the status of the ws server
- Add output to log more detailed information about the occupied memory and the number of current connections
- Make a check for operating system and develop a version for Denver and Windows
- Make authorization
But, in my case, the task was to make myself a simple tool for monitoring the state of the daemon via the web. By the way, with a little modification this thing can be used to monitor any daemon, not just ws.
As for the implementation for Windows, all places where pid can be bypassed by checking and thus ensure launchability.
If (strtoupper(substr(PHP_OS,0,3)) === "WIN") ( )
Whether the daemon is running or not, it is enough to check simply by the presence of the pid file.
While I was on the weekend, I forgot to turn off the ws echo server, as a result, its lifetime was 233774 seconds, i.e. somewhere around 3 days, the occupied memory remained about 0.1%, which indicates that the solution has the right to life.
Feedback, comments are always welcome. I will continue to develop the topic and implement a simple chat as promised.
My articles about PHP daemons and web sockets
- PHP web socket server, PHP daemon launch