Start WebSocket on Ubuntu - php

Need to start the (PHP)WebSocket on my Ubuntu server. its a working script at least on my PC (using WAMP server). Here is the code
<?PHP
include('php_web_socket.php')
$socket = new php_web_socket();
$socket->wsStartServer('127.0.0.1', 30001);
?>
I'm executing this php file by using the following command
php /var/www/html/server/index.php
But the WS connection never open.
For reference,
I'm using the following script http://www.sanwebe.com/2013/05/chat-using-websocket-php-socket

I was making a mistake. Look the IP 127.0.0.1 - Localhost
lets allow the remote connection from rest of the world and change it to 0.0.0.0 and problem resolved!

Related

How to use WebSocket in XAMPP server

How to enable websocket in xampp server? I use windows 10.
I want to build a chat based on websocket and but I can't connect to the websocket.
I found this article
http://phppot.com/php/simple-php-chat-using-websocket/ but I don't understand how to connect php xampp to websocket.
Uncomment extension=php_sockets.dll in php.ini file to enable websockets in php.
Simple.
Go to the project folder and run the "php php-socket.php" command on the terminal and the websocket server will works.
Make sure that the php path is in the environment variables, or run with full php path: "c:/xampp/full_path_to_php/php php-socket.php".
After running the server, access index.php with your browser and the chat will works.
php-socket.php is a script that creates a websocket service, class.chathandler.php is a chat implementation on the server side and index.php is the client side.
Please, sorry for my English.
I tried get to run the example too, with the same error. Following solution is working:
download the example code and copy to your htdocs folder.
open two tabs in your Browser. 1. run the php-socket.php and let it load. 2. run the index.php in the 2nd tab and the example will work.
I made some changes and got it working because I already have a website running on XAMPP.
"php-socket.php" → Change line 3:
define('PORT',"8090");
to:
define('PORT',"1337");
"php-socket.php" → Change line 11:
socket_bind($socketResource, 0, PORT);
to:
socket_bind($socketResource, HOST_NAME, PORT);
"index.php" → Change line 25:
var websocket = new WebSocket("ws://localhost:8090/demo/php-socket.php");
to:
var websocket = new WebSocket("ws://localhost:1337/demo/php-socket.php");
Make a .bat file inside this demo folder:
#echo off
"C:\xampp\php\php.exe" -q "C:\xampp\htdocs\demo\php-socket.php"
Launch the .bat file, and keep the cmd open as this is the socket listener or server; as soon as you close it, the frontend will say "Connection Closed"

local PHP & local node.js (react-native)

I have a local Apache server using XAMPP which runs on port 80 and uses a local PHP file.
In the browser I can open this file from localhost/test/test.php and localhost:80/test/test.php.
I also have a react-native app, and it starts an NPM server on port 8081.
In this app I want to make a request to the local PHP file, but when I make the request it returns a 404 error.
I understand that it happens because the NPM server doesn't know about the local Apache server so I want to understand how to combine them.
Thanks.
don't write 'localhost/test/test.php'
You have to write with your ip, like this => '192.16x.x.x/test.php/'

Unable to execute "testphp" from localhost

I installed LAMP from unixmen.com, but I am not able to run the php script using localhost. It says:
Not Found The requested URL /html/testphp.php was not found on this server. Apache/2.4.18 (Ubuntu) Server at localhost Port 80
Note! My apache server works fine.
Have you started your lampp server?
terminal command is
sudo/opt/lampp/lampp start
provided that is where lampp is installed.
Also the url should be http://localhost/name_of_your_project/testphp.php
first question: this file is first .php what executed in new serve?
case yes: make sure your server is started:
sudo/opt/lampp/lampp start
verify permission in your file.
or
try access using IP 127.0.0.1
http://127.0.0.1
your directory html are sub \var\www\html ?
try move to one level up
Regards

php exec not working properly on remote server

php functions like exec and shell_exec work fine on localhost but it doesn't work properly on remote host.
$output = shell_exec('dir');
echo "<pre>$output</pre>";
This code gives output in localhost but not at remote server.
Any Clue?
This kind of php commands are usually disabled on remote web servers. If you really want them to works, you should use a dedicated server and configure it yourself.
dir command is in windows system, so is your remote server a windows server too?
Even it is a windows server, the configure may be different from your local machine that disables shell_exec
Take a look at http://php.net/glob or http://php.net/manual/en/class.directoryiterator.php
Executing dir or ls makes no sense at all, to me.

Not able to launch putty with telnet session using php

I have putty.exe on my desktop so if I will go to command prompt and then to C:\Documents and Settings\user\Desktop location and execute following command it will launch the Putty window of telnet connection to server specified:
putty.exe telnet://a.b.c.d/
I am trying to launch the putty to telnet to specific server using following but its not working for me:
<?php
$securecrt = "C:\\Documents and Settings\\user\\Desktop\\putty.exe telnet://a.b.c.d/ ";
exec($securecrt);
?>
If I try following then it launches the putty application but will have to provide the host name or IP to login:
<?php
$securecrt = "C:\\Documents and Settings\\user\\Desktop\\putty.exe ";
exec($securecrt);
?>
Not sure if I am missing something very basic, It will be great if someone can help with this.
Instead of using Putty, you could try to use PHP with fsock or use a class like http://www.geckotribe.com/php-telnet/
However, you can try the following using your current setup:
$securecrt = '"C:\Documents and Settings\user\Desktop\putty.exe" telnet://a.b.c.d/';

Categories