Jump to content

Problem with HTML and PHP


Recommended Posts

Hi all,

Working with:

  • Linux Debian
  • Lighttpd Server
  • PHP7.0

I want to launch some shell scripts pressing HTML buttons. All the files are placed in /var/www/html The rights are done by:

sudo chown www-data:www-data /var/www/

So, I wrote a very simple code:

HTML test code (index.html):

<form action="openApp.php">
    <input type="submit" value="Open Script">
</form>

PHP test code (openApp.php):

<?php
    exec('test.sh');
    header('Location: http://localhost/index.html?success=true');
?>

But no luck, nothing is working.

Note that the shell script just create an empty file like that:

touch /home/$USER/testFile

Also, my Lighttpd configuration file is like that:

server.modules = ( "mod_access", "mod_alias", "mod_compress", "mod_fastcgi", "mod_redirect", )

May be I am wrong with that piece of code. Do I need some Javascript to do the job ? May be the rights are wrong ?

Could you help me please ? I am stuck with that.

Thank you for your help and support

Link to post
Share on other sites

I tried your solution, but I am stuck with that.
Nothing works !
 

If my HTML page is like that with the post method:

<form action="openApp.php" method="post">
    <input type="submit" value="Open Script">
</form>

Is that correct to call the PHP script ?
Or maybe using type=button is better than submit ?

All the files (HTML, SHELL and PHP) are managed by the www-data user.
Is that correct too ?

Another point:

When I go to /var/www/html, and launching just my PHP script:

cd /var/www/html
php openApp.php

the script works fine, and write something in a file text like this:

<?php
	$myfile = fopen("/home/$USER/file.txt", "w") or die("Unable to open file!");
    $txt = "writing test\n";
    fwrite($myfile, $txt);
	fclose($myfile);
?>

But when I use the HTML page, nothing works.
So I deduce that is a permission access problem right ?
Because the PHP script works, and the HTML works too.
So, where I am wrong ?

Edited by cherault
Link to post
Share on other sites

This is the logs...I have some problems with the rights access:

COMMAND  PID     USER   FD      TYPE DEVICE SIZE/OFF NODE NAME
lighttpd 716 www-data  cwd   unknown                      /proc/716/cwd (readlink: Permission denied)
lighttpd 716 www-data  rtd   unknown                      /proc/716/root (readlink: Permission denied)
lighttpd 716 www-data  txt   unknown                      /proc/716/exe (readlink: Permission denied)
lighttpd 716 www-data NOFD                                /proc/716/fd (opendir: Permission denied)

 

Link to post
Share on other sites

Ok, doesn't work...I don't what to do.
I though it xas simple to build an HTML interface to write some data within a text file, but it isn't !!!!
This is why I love C++ 😉

More seriously, is there is a way to understand what's going on with HTMl and PHP.
I admit i'm really stuck with that !!!

Thanks for your help and support

Link to post
Share on other sites

Ok, I tried your solution like this:

<?php
	$myfile = fopen("/var/www/html/newfile.txt", "w") or die("Unable to open file!");
    $txt = "testwriting\n";
    fwrite($myfile, $txt);
	fclose($myfile);
?>

The HTML file is like that:

<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<form action="openApp.php">
    <input type="submit" name ="value" value="Open Script">
</form
</body>
</html>

But it still doesn't work.

The access are the next:


-rw-r--r-- 1 www-data www-data  173 déc.   6 12:53 index.html
-rw-r--r-- 1 www-data www-data    0 déc.   6 12:34 newfile.txt
-rwxrwxr-x 1 www-data www-data  174 déc.   6 13:06 openApp.php

What do you think about this ?

Link to post
Share on other sites

Thanks Luisrosety,

I did it but nothing appears...

The php script is :

<?php
	ini_set('display_errors', 1);
	ini_set('display_startup_errors', 1);
	error_reporting(E_ALL);

    exec("/home/$USER/script");
?>

Is it possible that the web browser (Firefox) block some informations ?

Edited by cherault
Link to post
Share on other sites

You should use single-quotes in the exec() command, otherwise it thinks that $USER is a PHP variable. I would expect the above code to throw an error similar to "Undefined variable $USER".

Link to post
Share on other sites

Thanks Ingolme for the info. But it doesn't solve my problem.

I am sure the HTML and PHP code are correct.
When I launch the PHP in console mode, everything works fine.

But when I launch it through HTML...nothing happen.
So, I suppose there is some problems with the permissions, of Firefox is blocking something....I really don't know.
I just want to activate a shell script from an html button...I must be easy no ?
 

Link to post
Share on other sites

I have tried the following:
1.The php that directly writes the file on the same directory (/home/pi/www/test).

2.  Navigate with the browser to the directory http://myserver/test and the file is not created and php gives the error: 

Warning: fopen(file.txt): failed to open stream: Permission denied in /home/pi/www/html/test/index.php on line 6
Unable to open file!

 

3. Then I give "rwx" permission to others:

chmod o+wx /home/pi/www/test

4. Then going to the same directory http://myserver/test the file gets created

The php file (index.php)

<?php 
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    $myfile = fopen("file.txt", "w") or die("Unable to open file!");
    $txt = "writing test\n";
    fwrite($myfile, $txt);
    fclose($myfile);
?>

I have done the tests with lighttpd.
Launching the creation of the file directly with the script gives more information.
You must have permission issues.

Link to post
Share on other sites

Hi Luisrosety,

Thank you for the help.

I did all the steps you gave me, but no luck.
Who is the user in /home/pi/www/test ?
Is it root or www-data ?

Actually, I delete and reinstall lighttpd and php7.0.
The actual permissions and owner are root.
Do I need to move to www-data like that :

sudo chown -R www-data:www-data /var/www/html

or

sudo chown -R <user>:www-data /var/www/html

 

Edited by cherault
Link to post
Share on other sites

Ok, some news from my tests:

I did that:

sudo usermod -a -G www-data <user>
sudo chgrp -R www-data /var/www/html/
sudo chmod -R g+w /var/www/html/

After these modifications, when I launch the php script, I saw :

Notice: Use of undefined constant  - assumed '' in /var/www/html/test.php on line 9

But the file was written in /var/www/html
So I can say this solve the problem partially.

Whatever, Thank you to all of you for your time, help and support

Regards,

 

Link to post
Share on other sites

At least, one question:

Using the base of the php script for writing a file, and using the same HTML page:

PHP (test.php):

<?php 
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    $myfile = fopen("file.txt", "w") or die("Unable to open file!");
    $txt = "writing test\n";
    fwrite($myfile, $txt);
    fclose($myfile);
?>

HTML (index.html):

<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<form action="test.php" method="POST">
    <input type="submit" name="Envoyer" value="Open Script">
</form
</body>
</html>

I tried to modify the PHP file to launch a shell script :

SHELL (test.sh)

#!/bin/bash

cd /home/$USER/Prog/C++/Debug/
./test

exit 0

So my PHP file look like this:

PHP (test.php)

<?php 
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

	exec("test.sh");
	header('Location: http://localhost/index.html?success=true');
?>

But it doesn't work !
I have the same permissions as before, so the rights access are corrects.
So I think it comes from the PHP script, but I don't know why it doesn't work.

Do you have an idea ?

Thanks for all

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...