Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

How to use exec shell with PHP and MySQL?

I want to use an exec shell in PHP code by connecting to a database. I had used this block of code but it doesn't work! I don't know what might be the problem, so this is my code:

if(isset($_POST['submit4']))

{
$results = shell_exec("cat /var/log/apache2/access.log | grep" . $_POST['key'] . "| sed s/'^.*apache2'/''/g | sort | uniq -c | sort -nr | cat ");
echo $results ;

}

else if(isset($_POST['submit5']))
{
mysql_connect('127.0.0.1',"root"," ") or die("erreur de connexion au serveur");
    mysql_select_db("lastnline");
        $sql='Select * from motclef';    
    $res=mysql_query($sql);
           while($row=mysql_fetch_array($res))

    {
$results = shell_exec("cat /var/log/apache2/access.log | grep" . $row['nom'] . "| sed s/'^.*apache2'/''/g | sort | uniq -c | sort -nr | cat ");
echo $results ;

    }       
}

The submit4 works very good but the submit5 doesn't work :/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

But never populate input values directly into a shell command. Use the function escapeshellarg():

$key = escapeshellarg($_POST['key']);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...