PHP - Execute Program In Terminal By Php & Work With Input/output
Similar TutorialsHi everyone, i'm trying to get it so when i visit my webpage called start.php it executes the command (/bigbrotherbot start) in the terminal which then should start the process on my linux machine. I'm currently using this code Code: [Select] <?php shell_exec('sudo /bigbrotherbot start'); ?> This script doesn't appear to do anything though. I'm very new to php and would really appreciate the help. P.S the file bigbrotherbot is the bash script which works if i actually go to the terminal myself and type /bigbrotherbot start yet when i try to make the php do it it doesnt do anything. any ideas? I need to execute a program in the command prompt in windows using php, which php functions would be best to do this? and would i need to set any type of permissions to be able to use them? Hi there,
My friend asked me to help him perform a task today and I was completely stumped as to how I should complete it. Being someone who is reasonably experienced with PHP programming I didn't think I would find it absolutely impossible to complete, maybe hard, but not impossible. So I wondered if someone would now be able to explain to me in order to assist me in expanding my knowledge how to complete this task.
https://www.dropbox....B8M6cr_z7a?dl=0
Here is a link to the files he sent me, please ignore the C++ section.
If anyone could help / begin to give an explanation please fire away. I feel so bad about myself for NOT knowing that I'm now motivated to learn more!
Many thanks!
After inputing my email and password and clicking submit, the echo in Verify_User (in bold) doesn't show up, nor does it load the logged in form of the log in bar. If I reload the page or press submit again (even without entering any information), everything works. At first I though I might just be outputting before getting my input on the reload, but all info is obtained at the top. I also thought it might be $logoff, but it doesn't tell me "You logged off!", while still setting $logoff to true by the end in order to output the login menu. Therefore, it get's passed if($Logoff) but doesn't make it to Verify_user, so for some reason $L_Email and $L_Password aren't set after the first refresh. I'm a newbie so any help, related to the problem or not, would be greatly appreciated. P.S. The logoff button works the first time. Code: [Select] <?php $L_Email = $_POST['l_email']; $L_Password = $_POST['l_password']; $Logoff = $_POST['logoff']; //if the page was requested with an effort to logoff if ($Logoff) { //setcookie("session_email", "", time()-3600); setcookie("session_password", "", time()-3600); } //if logging in, Email and password are already set, make them cookies else if($L_Email && $L_Password) { setcookie("session_email", $L_Email, 0); setcookie("session_password", $L_Password, 0); } $L_Email = get_email_cookie(); $L_Password = get_password_cookie(); $logbar_html = ""; if($Logoff) { $logbar_html = $logbar_html . "You logged off!<br>"; } else { //if there was a cookie or apt input if ($L_Email && $L_Password) { //if logged on if(verify_user($L_Email, $L_Password)) { $logbar_html = $logbar_html . "Hello $L_Email!<br>"; $logbar_html = $logbar_html . "<form action='index.php?at=". $_GET['at'] . "' method='post'> <input name='logoff' type='submit' value='logoff'> </form>"; $logbar_html = $logbar_html . "<a href='index.php?at=edit_account'><text size=1>Edit your account!</text></a><br>"; if ($L_Email == "scott.jaromin@gmail.com") { echo "<a href='index.php?at=admin'>Admin</a>"; } } else { $logbar_html = $logbar_html . "Login Failed<br>"; $Logoff=true; $L_Password=NULL; } } //if this, you are definitely logged off else { $Logoff = true; } } //if you are logged off if($Logoff) { $logbar_html = $logbar_html . "<a href='index.php?at=register'><text size='0'>Register!</text></a><br> <form action='index.php?at=". $_GET['at'] . "' method='post'> Email: <input name='l_email' type='text' value='$L_Email'/><br> Password: <input name='l_password' type='password' /> <br> <input type='submit' /> </form> "; } function get_email_cookie() { $email_cookie = ""; if(isset($_COOKIE["session_email"])) { $email_cookie = $_COOKIE["session_email"]; } return $email_cookie; } function get_password_cookie() { $password_cookie = ""; if(isset($_COOKIE["session_password"])) { $password_cookie = $_COOKIE["session_password"]; } return $password_cookie; } function verify_user($email_check, $pass_check) { echo "verifying $email_check $pass_check"; $con = mysql_connect("my", "info", "only"); if (!$con) { die("Could not connect: " . mysql_error()); } mysql_select_db("begood4u_barter", $con); if(!$email_check || !$pass_check) { return false; } $result = mysql_query("SELECT Password FROM Members WHERE Email='$email_check'"); $real_pass = ""; if($row = mysql_fetch_array($result)) { $real_pass = $row['Password']; } else { echo "$result"; } echo " real pass: $real_pass"; mysql_close($con); return($pass_check == $real_pass); } ?> I'm kind of a beginner, but I really need this script and I'm not sure how quite to do it as needed... With some html forms I need it so you can type a name in then click submit, then (with php) it will save that text into a text file or something. Later, when I hit a different button it will randomly display one of those texts/names from before. function showCart() { global $db; $cart = $_SESSION['cart']; $output[] = '<div align="center"><table cellpadding="1" cellspacing="1" bgcolor="#666666" width="400">'; $output[] = '<tr bgcolor="#FFFFFF" valign="middle">'; $output[] = '<td width="70" align="center">Titlu</td>'; $output[] = '<td width="90" align="center">Imagine </td>'; $output[] = '<td width="60" align="center">Pret</td>'; $output[] = '<td align="center">Sterge Produs</td>'; $total += $price * $qty; $output[] = '</tr>'; $output[] = '</table>'; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="http://www.sprayart.info/new/sprayart.php"><div align="center" name="trimite" id="trimite"><table width="400">'; foreach ($contents as $id=>$qty) { $sql = 'SELECT * FROM com_images WHERE id = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<tr bgcolor="#FFFFFF">'; $output[] = '<td width="70"> <input type="hidden" name="title[ ]" value="'.$title.'" size="5" maxlength="5" /> <input type="text" value="'.$title.'" size="5" maxlength="5" disabled="disabled" /></td>'; $output[] = '<td width="90" align="center" valign="middle"><img src=../../'.$thumbnail.'> </td>'; $output[] = '<td width="60" align="center">'.$price.'</td>'; $output[] = '<td ></td>'; $total += $price * $qty; $output[] = '<td width="50"><a href="cart.php?action=delete&id='.$id.'" class="r"><img src="../../media/sterge.jpg"></a></td>'; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<hr>'; $output[] = '<p align="right">Total De plata: <strong>'.$total.' RON</strong></p></div>'; $output[] = '<table border="0" width="444" id="table29" cellspacing="0" cellpadding="0" background="#BASE_URL#media/comand_form.gif" height="322"> <form action="#BASE_URL#sprayart.php" method="post" name="trimite" id="trimite"> <tr> <td width="25"></td> <td width="71" class="text_negru_left_25"><span class="style4">*</span> Nume:</td> <td width="146"><input name="nume" type="text" class="text_negru_fild_19" /></td> <td width="170"></td> <td width="32"></td> </tr> <tr> <td width="25"></td> <td class="text_negru_left_25"><span class="style4">*</span> Adresa:</td> <td ><input name="adresa" type="text" class="text_negru_fild_19"></td> <td width="170"></td> <td width="32"></td> </tr> <tr> <td width="25"></td> <td class="text_negru_left_25"><span class="style4">*</span> E-mail:</td> <td ><input name="email" type="text" class="text_negru_fild_19" /></td> <td width="170"></td> <td width="32"></td> </tr> <tr> <td width="25"></td> <td class="text_negru_left_25"><span class="style4">*</span> Telefon:</td> <td ><input name="telefon" type="text" class="text_negru_fild_19" /></td> <td width="170"></td> <td width="32"></td> </tr> <tr> <td width="25" height="139"></td> <td colspan="3" class="text_negru_left_25"><span class="style4">*</span> <textarea name="mesaj" cols="44" rows="7" ></textarea></td> <td width="32"></td> </tr> <tr> <td width="25" height="39"></td> <td colspan="2" valign="top" class="text_negru_left_25"></td> <td valign="top" class="text_negru_left_25"><input style="BORDER-RIGHT: #3a3a3a 1px solid; BORDER-TOP: #3a3a3a 1px solid; FONT-SIZE: 8pt; FLOAT: right; BORDER-LEFT: #3a3a3a 1px solid; COLOR: #b1b1a7; BORDER-BOTTOM: #3a3a3a 1px solid; FONT-FAMILY: Verdana; BACKGROUND-COLOR: #3a3a3a" type="submit" class="buton" name="trimite" value="Trimite" /></td> <td width="32"></td> </tr></table></form>'; how can i get the "title" for all products ! if i buy one its ok it will send me in email, but if is more products i get only the last added product!! Please help thank you ! I am trying to work on a code for php where you first have a form that asks the user to input a message then input a color. When they click the submit button it takes them to the output page. It should take their message and output it into boxes in a square. And the background should turn the color that they types in. I'm working on a basic structure for websites containing user management and a template engine. I've run into troubles with the template engine when I try to allow for including scripted pages. This function is supposed to include and buffer the scripted page: function parse_file($file) { ob_start(); include($file); $buffer = ob_get_clean(); return $buffer; } In my template class I check to see if the data entered is a file or not, if it is then I run these lines to replace place holder-tags with the output buffer of the file included: foreach ($tags as $tag => $data) { if (file_exists($data)){ $data = $this->parse_file($data); } $this->page = preg_replace('/{' . $tag . '}|<!-- '.$tag.' -->/', $data, $this->page); } now here is the hitch, it seems the preg_replace is only replacing one of several identical place holder-tags and I am puzzled as of why this happens, could it be the buffer or what? Hello house, I am new to coding, I have a project about generating school terminal results and using pin to check results in codeigniter framework. Any help will be appreciated. Thanks
I am trying call an R batch file inside php script from browser. The R script basically generate an imgae and save in a folder. Unfortunately, I could call the R within php (means that no image is saved in the folder) using BROWSER. But when I run the php script from the commadline, it works and image is saved in the folder.
<?php exec("Rscript /var/www/html/phil/figs/map.R"); ?>Execuition of following R batch code and php codes work and save image in the specified folder from the terminal. 58A-UD3R:~$ php /var/www/html/phil/x.php 58A-UD3R:~$ Rscript /var/www/html/phil/figs/map.RI totally in confusion why browser cannot successfully save image. Or is there anythinng need to be change in server for generating image? Anyone please help me for solving this probelm. Thank You..
For example with an IDE to run a program is there a way to put some code in the path and let it tell me where it came from like a referral? $ref = $_SERVER['HTTP_REFERER']; echo("$ref<br>"; Result: https://www.mydomain.com/wp-admin/admin.php?page=lambo As you can clearly see the page that we have entered the code in is referred by the URL here. It tells me that it was last at a word press page /wp-admin/admin-php and I can go to that page and it will be the last page it was at.. So bottom line then if i add this code to the admin page and click the same button i had before.. it will tell me the file that called out the admin page. Baslcaly I am looking for ideas on how to track the page i am now all the way back to the page with the button on it and all intermediate pages that it passes thru to get to the final destination. It seems that to succesfully troubleshoot a fault is to follow the path of the entire job and see if any breaks... Any thoughts or suggestions on how to best do this would greatly be appreciated.
Hello everyone. I need help with the following PHP APP. I am running on (Version PHP 7.2.10) I am trying to have a page table form on table.php pass the input variable of “5-Numbers” to another page called table_results.php I want that variable string of “5-Numbers” to be compared against 4 arrays and output any duplicates found within each of those 4 lists. If nothing is found, I still want some visual output that reads “None found”.
Lets pretend I have the following example .. On table.php, I typed inside my table form the 5-Numbers .. INPUT: 2,15,37,13,28 On table_results.php, the 4 arrays to be compared against my input numbers “ 2,15,37,13,28” are ..
$array_A = array(2,6,8,11,14,18,24); $array_B = array(1,2,9,10,13,14,25,28,); $array_C = array(1,3,7,9,13,15,20,21,24); $array_D = array(4,5,12,22,23,27,28,29);
So my output should read as follows below .. OUTPUT:
TABLE COLUMN 1 COLUMN 2 ROW 1 Matches Found Results .. ROW 2 GROUP A: 2 ROW 3 GROUP B: 2,13,28 ROW 4 GROUP ? 13,15 ROW 5 GROUP ? 28 ROW 6 5#s Input: 2,15,37,13,28
Please let me know if anyone has any suggestions on how to go about it. Thanks. Edited January 1, 2019 by Jayfromsandiego Wanted to include image example Code: [Select] <?php ob_start(); session_start(); require_once ("functions.php"); $returnurl = urlencode(isset($_GET["returnurl"])?$_GET["returnurl"]:""); if($returnurl == "") $returnurl = urlencode(isset($_POST["returnurl"])?$_POST["returnurl"]:""); $do = isset($_GET["do"])?$_GET["do"]:""; $do = strtolower($do); switch($do) { case "": if (checkLoggedin()) {include("usernav.php");?> <h3>User Logged In</h3> <p>This is the members only area.</p> <?php } else {?> <a name="User Panel"></a> <h1>User Panel</h1> <h3>User Login</h3> <form name="login" action="login.php?do=login" method="POST" onsubmit="return aValidator();"> <input type="hidden" name="returnurl" value="<?$returnurl?>"> <table cellPadding=3 width=225 border=1> <tr> <td colSpan=2> <center> <p><b>Members-Only Area!</b></font></p> </center></td></tr> <tr> <td align=center width=70>Username:</td> <td><input type="text" name="username"></td></tr> <tr> <td align=center width=70>Password:</td> <td><input type="password" name="password"></font></td></tr> <tr> <td align=middle colspan=2> <input type="submit" name="submit" value="Login"></td></tr> <td align=middle colSpan=2> <input type="checkbox" name="remme" value="on"> Remember me for the next time I visit</td> </form></table></td> <?php } break; case "login": $username = isset($_POST["username"])?$_POST["username"]:""; $password = isset($_POST["password"])?$_POST["password"]:""; if ($username=="" or $password=="" ) { echo "<h1>Username or password is blank</h1>";//this does not show when the input(s) are blank clearsessionscookies(); header("location: index.php?returnurl=$returnurl"); } else { if(confirmuser($username,$password)) { createsessions($username,$password); if ($returnurl<>"") header("location: $returnurl"); else { header("Location: index.php"); } } else { echo "<h1>Invalid Username and/Or password</h1>";//this message does not show when the user login invalid clearsessionscookies(); header("location: index.php?returnurl=$returnurl"); } } break; case "logout": clearsessionscookies(); header("location: index.php"); break; } ?> What maybe the case, could anyone help me with this? Thanks Ted. Well I have a script that executes a scan on a system set to run infinitely, and I need it to echo out a message each time it loops through, but I don't want it to echo out the message with the next loop message below it, and the next one below that etc... I've tried using the flush(); function and been messing around with that with no luck. For security reasons I don't want to release any of the processing code, but here is the basic construction of the script: <?PHP ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** $RepeatIt = -1; for($g=1; $g!=$RepeatIt+1; $g++) { ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** $ScanMessage = ":.:.: SCANNING THE HITLIST FOR MOBSTER: ".$MobName." (SCAN #$g) :.:.:"."<br/><br/>"; echo $ScanMessage; ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** ***PROCESSING AND SCAN CODE*** } ?> At the moment it's returning: :.:.: SCANNING THE HITLIST FOR MOBSTER: DEUS EX DESTROYER (SCAN #1) :.:.: :.:.: SCANNING THE HITLIST FOR MOBSTER: DEUS EX DESTROYER (SCAN #2) :.:.: :.:.: SCANNING THE HITLIST FOR MOBSTER: DEUS EX DESTROYER (SCAN #3) :.:.: :.:.: SCANNING THE HITLIST FOR MOBSTER: DEUS EX DESTROYER (SCAN #4) :.:.: So what I want it to do is just delete the scanning message and replace it with the next scan message so while running this script you would see just the number increment on the same line. Any suggestions? Thanks. Hi Guys, I have the following script which works fine for GoogleMaps Code: [Select] <?php require'EasyGoogleMap.class.php'; $gm = & new EasyGoogleMap("rYvyXhRFPYW5tkV_IE5hWxecUidHAYjJhBSp59xzQuNZcoxNYCiaum4_Xb66Fw"); $gm->SetMarkerIconStyle('STAR'); $gm->SetMapZoom(10); $gm->SetAddress("10 market st, san francisco"); $gm->SetInfoWindowText("This is the address # 1."); $gm->SetAddress("Manila, Philippines"); $gm->SetInfoWindowText("This is Philippine Country."); $gm->SetSideClick('Philippines'); ?> <html> <head> <title>EasyGoogleMap</title> <?php echo $gm->GmapsKey(); ?> </head> <body> <?php echo $gm->MapHolder(); ?> <?php echo $gm->InitJs(); ?> <?php echo $gm->GetSideClick(); ?> <?php echo $gm->UnloadMap(); ?> </body> </html> I've now coded that static page to take the following, and within the GoogleMaps database field entered in exactly the following $gm->SetAddress("10 market st, san francisco"); $gm->SetInfoWindowText("This is the address # 1."); $gm->SetAddress("Manila, Philippines"); $gm->SetInfoWindowText("This is Philippine Country."); $gm->SetSideClick('Philippines'); Code: [Select] <?php $maps_find = $OO_Open->newFindCommand('Maps'); $maps_find->AddFindCriterion('AccountName','xx'); $maps_result = $maps_find->execute(); $maps_row = current($maps_result->getRecords()); $Gdata = $maps_row->getField('GoogleMaps'); //echo $Gdata; //exit; ?> <?php require'EasyGoogleMap.class.php'; $gm = & new EasyGoogleMap("my key"); $gm->SetMarkerIconStyle('GT_FLAT'); $gm->SetMapZoom(10); $gm->SetMapWidth(1080); # default = 300 $gm->SetMapHeight(550); # default = 300 echo $maps_row->getField('GoogleMaps'); //eval("\$Gdata = \"$Gdata\";"); ?> <html> <head> <title>EasyGoogleMap</title> <?php echo $gm->GmapsKey(); ?> </head> <body> <?php echo $gm->MapHolder(); ?> <?php echo $gm->InitJs(); ?> <?php echo $gm->GetSideClick(); ?> <?php echo $gm->UnloadMap(); ?> </body> </html> Is there way of getting the $maps_row->getField('GoogleMaps'); command to behave as if it was php (when I echo it, it does display the code as text). I'm not using the FileMaker db to store the code, I thought eval function was the answer but not had much luck. Many Thanks for the advice Jalz Hi, I am trying to create a PHP script ("repair-correct.php") in order to run some CLI commands without using PuTTy - The CLI commands are needed to repair/correct the execution of a web application named Mautic. Shared web host account with PHP 7.0 URL: https://www.myserver.com Mautic directory: https://www.myserver.com/mautic
What I want to do is:
Step1: Change ownership of files and folders To find out which user Apache is running as, I want to execute the following command and take note of the first entry in the line which is returned: ps aux | grep apache2 I want to use this information to find the groups with the following command groups apache_user (where apache_user is the user I identified from the first step above) To reset the ownership of files and folders, I want to use the following command (ensuring that I replace apache_user and apache_group with the values identified in the steps above): sudo chown -R apache_user:apache_group With this command I want to change ownership, using the -R flag which means recursively - including all files/folders within that location.
Step 2: Reset the file and folder permissions
find . -type f -not -perm 644 -exec chmod 644 {} +
Thanks so much for your help in solving this problem!
Best, Tony
I have written a Validation class that checks to see if a file being uploaded to the server meets certain conditions. That works a treat. The next step is to actually upload it to the server and I have an Upload class that can do that. Again, that works perfectly fine. Once the file uploads, I am passing the $location of that of that file to my DB class. The DB class is full of methods that prepare and then execute strings that are SQL queries that are required in other areas of my application. Nothing I have at the moment is suitable for just running an SQL file so I don't know what to do now.. My procedural code, that works, looks like this; $dbh2 = new PDO("mysql:host=localhost;dbname=DB360transfer", $login_user, $login_password); $sql = file_get_contents($path.$new_file_name); $qr = $dbh2->exec($sql);I'm not sure how to replicate this up in a PDO/OOP application. My DB Class script is attached. The run_from_file code starts on line 98. I've left in the other stuff as I suspect the answer has something to do with using the $this or the self:: - but really I have no idea. So my question is, what is the correct syntax for executing a file in OOP? Attached Files DB.php 2.92KB 6 downloads Not sure if this is possible I am trying to do a multi delete: Code: [Select] <form name="form1" method="post" action=""> <table> while($rows=mysql_fetch_array($result)){ ?> <tr> <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td> <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td> </tr> <?php } ?> // Check if delete button active, start this if($delete){ for($i=0;$i<$count;$i++){ $del_id = $checkbox[$i]; $sql = "DELETE FROM $tbl_name WHERE id='$del_id'"; $result = mysql_query($sql); } <tr><td> <select name="dropdown"> <option value="option1">Choose an action...</option> <option value="edit">Edit</option> <option value="delete">Delete</option> </select> <a class="button" href="#">Apply to selected</a> if($result){ echo "<meta http-equiv=\"refresh\" content=\"0;URL=nameslist.php\">"; } } mysql_close(); Hi, Below i have some sample code and am confused over execute(), i have the code below in a try and catch block, in the catch block i call a function i created to log any error that is caught in catch block to a .txt file. I then looked online and it seems that i should do an if statement check on execute to ensure it executed the query, the part that confuses me if the execute failed i thought it would be caught in the catch block but it seems that is not the case. To explain better i have commented the code in depth on the area that i am confused about. Any help in making me understand would be great. I have not included all code above try and catch to keep things simple Code: [Select] <?php try { // connect to database $dbh = sql_con(); // checke if username exists on users table or users banlist table $stmt = $dbh->prepare(" SELECT users.user_login FROM users WHERE users.user_login = ? UNION ALL SELECT users_banlist.user_banlist FROM users_banlist WHERE users_banlist.user_banlist = ?"); // this is the part i am confused with, why is it i would use an if statement on execute() ? // i thought using a try and catch block any errors would be caught in the catch block. // using an if statement to check if execute() worked, i thought if execute failed it would // be handled by the catch block, i mean in my exmaple code here, what could cause the execute to fail ? // and why if execute failed it would not be caught by catch block ? // i am looing at exmaple online and i am reading different things and its all confusing me // execute query if(!$stmt->execute(array($username, $username))){ echo 'something went wrong .. '; } else { // execute worked } } // if any errors found log them in my ExceptionErrorHandler() function and display friendly message catch (PDOException $e) { // this function catches an error and logs them to file ExceptionErrorHandler($e); require_once($footer_inc); exit; } } ?> Thanks or any help! How can I get this form to execute? Do I need to use a hidden input or something? Code: [Select] <form action="partnerRequest.php" method="post"> <a href="" title="Become Associates" id="becomeassoc" class="mll"> <span class="f_right">Become Associates</span><img src="../assets/img/becomeassoc.jpg" class="mrs vmiddle" alt="Become Associates" /> </a> </form> |