How to exec the shell script in PHP - php

I'm making PHP script to move shell script. when hoge < 50.
Code is this. Does it OK?
When I use that script , I click this link.
http://localhost/index.php?hoge=30
<?php
if(isset($_GET['hoge'])) {
$hoge = $_GET['hoge'];
print("$hoge<br>\n");
}
if ($hoge > 50 ){
echo 'F1';
}
elseif ($hoge == 50) {
echo 'F2';
} else {
echo 'F3';
exec(' /Users/hoge/Desktop/test.sh');
}
?>

You can add a test like
if (file_exists('/Users/hoge/Desktop/test.sh')) {
exec('/Users/hoge/Desktop/test.sh');
} else {
echo 'NO FILE'
}
Check also that the file /Users/hoge/Desktop/test.sh is executable

Related

Changing links by priority - PHP / HTML

I am currently working on a school project and appreciate your help.
HTML Code:
LINK 1
LINK 2
LINK 3
Now as I am relatively new to PHP, I need clue wehere to start or which topics are relevant for my solution. I have already searched the Forum here and with google, but unforutenly, didn't find anything, that fits my needs.
What I need, is a PHP-Code, that contains 6 href Links and every Link has a priority. So every time somebody clicks on "LINK 1" on the html file, he gets directed to the PHP-File "change-1.php", where one of the six href Links get activated.
PHP CODE "change-1.php":
<?php
if (condition) {
echo "http://www.someotherwebsite-1.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-2.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-3.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-4.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-5.com";
}
elseif (condition) {
echo "http://www.someotherwebsite-6.com";
}
?>
Perhaps this should help
LINK 1
LINK 2
LINK 3
Just create one change.php file
//to get change
<?php
if ($_GET['change'] == 1) {
echo "http://www.someotherwebsite-1.com";
}
elseif ($_GET['change'] == 2) {
echo "http://www.someotherwebsite-2.com";
}
elseif ($_GET['change'] == 3) {
echo "http://www.someotherwebsite-3.com";
}
elseif ($_GET['change'] == 4) {
echo "http://www.someotherwebsite-4.com";
}
elseif ($_GET['change'] == 5) {
echo "http://www.someotherwebsite-5.com";
}
elseif ($_GET['change'] == 6) {
echo "http://www.someotherwebsite-6.com";
}
?>
Now this is the basic idea. You can manipulate the data however you want.
I hope you got the concept.

If-else condition gives me errors in PHP

I have two websites, each with it's own domain name. On load I have execute a php file that is used on both sites and has the same functionality.
Here is the code of that file:
<?php
echo '1';
if ($_SESSION["template"]=="template1";)
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template1/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template1/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template1/c.php');
}
else if ($_SESSION["template"]=="template2";)
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template2/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template2/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template2/c.php');
}
else {
echo 'NO DATA';
}
echo '4';
?>
On each of the two sites I set a session variable but in the above code it doesn't seem to work as I expect it to.
Am i missing something?
remove semicolon from if() and else if() statement, also add brackets when you using nested if else because it makes someone to understand easier and looks better
<?php
echo '1';
if ($_SESSION["template"]=="template1")
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template1/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template1/b.php');
}
else if { ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template1/c.php');
}
}
else if ($_SESSION["template"]=="template2")
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template2/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template2/b.php');
}
else if ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template2/c.php');
}
}
else {
echo 'NO DATA';
}
echo '4';
?>
After reviewing your code I edited my answer. The below code will do exactly the same as your code but requires alot less code.
<?php
if (isset($_SESSION['template']) && isset($_REQUEST['cmdAction'])) {
echo file_get_contents('http://localhost/images/'.$_SESSION['template'].'/'.strtolower($_REQUEST['cmdAction']).'.php');
} else {
echo 'NO DATA';
}
?>

Clear CMD-shell with php

I have this simple php script which outputs a string every second.
<?php
$i = 1;
while(1)
{
exec("cls"); //<- Does not work
echo "test_".$i."\n";
sleep(1);
$i++;
}
I execute the script in the command shell on windows (php myscript.php) and try to clear the command shell before every cycle. But I don't get it to work. Any ideas?
How about this?
<?php
$i = 1;
echo str_repeat("\n", 300); // Clears buffer history, only executes once
while(1)
{
echo "test_".$i."\r"; // Now uses carriage return instead of new line
sleep(1);
$i++;
}
the str_repeat() function executes outside of the while loop, and instead of ending each echo with a new line, it moves the pointer back to the existing line, and writes over the top of it.
can you check this solution
$i = 1;
echo PHP_OS;
while(1)
{
if(PHP_OS=="Linux")
{
system('clear');
}
else
system('cls');
echo "test_".$i."\n";
sleep(1);
$i++;
}
Apparently, you have to store the output of the variable and then print it to have it clear the screen successfully:
$clear = exec("cls");
print($clear);
All together:
<?php
$i = 1;
while(1)
{
$clear = exec("cls");
print($clear);
echo "test_".$i."\n";
sleep(1);
$i++;
}
I tested it on Linux with clear instead of cls (the equivalent command) and it worked fine.
Duplicate of ➝ this question
Under Windows, no such thing as
#exec('cls');
Sorry! All you could possibly do is hunt for an executable (not the cmd built-in command) like here...
You have to print the output to the terminal:
<?php
$i = 1;
while(1)
{
exec("cls", $clearOutput);
foreach($clearOutput as $cleanLine)
{
echo $cleanLine;
}
echo "test_".$i."\n";
sleep(1);
$i++;
}
if it is linux server use following command (clear)
if it is window server use cls
i hope it will work
$i = 1;
while(1)
{
exec("clear"); //<- This will work
echo "test_".$i."\n";
sleep(1);
$i++;
}
second solution
<?php
$i = 1;
echo PHP_OS;
while(1)
{
if(PHP_OS=="Linux")
{
$clear = exec("clear");
print($clear);
}
else
exec("cls");
echo "test_".$i."\n";
sleep(1);
$i++;
}
This one worked for me, tested also.

need help echo in index.php from process.php

After my form has been submitted and my message has been sent I want to go back to the index which this does, but I also want to display echo 'Message has been sent' in the index.php in a div instead of my process.php, how would I go about doing this. Sort of new to php if you need any more of my code I will provide or link to site. Thanks.
This is what i tried so far.
in my process.php file
if(!$mail->send()) {
$output = 1;
// $output = 'Mailer Error: ' . $mail->ErrorInfo;
} else {
$output = 2;
header('Location:index.php');
}
in my index.php file
<?php
if ($output == 2) {
echo "<b>Message has been sent</b>";
} elseif ($output == 1) {
echo "<b>Message could not be sent, please try again</b>";
} else {}
?>
The variable $output isn't set in your index.php file.
As an easy way to start you can try it like
header('Location:index.php?output='.$output);
and fetch output in your index.php with
$output = $_GET['output'];
at the beginning of your file, that way you will be able to make your if statements work.
Also be advised that you will never be redirected if $output = 1 in your process.php, as the header is only in the else statement. Simply place the header after your else statements closing bracket.
if(!$mail->send()) {
$output = 1;
} else {
$output = 2;
}
header('Location:index.php?output='.$output);
die();
index.php:
<?php
if (isset($_GET['output'])) {
$output = $_GET['output'];
if ($output == 2) {
echo "<b>Message has been sent</b>";
} elseif ($output == 1) {
echo "<b>Message could not be sent, please try again</b>";
}
}
Please be advised that you shouldn't use unsanitized request data (user input and what so ever) in a production environment, as this is a security risk.
This won't work, the value won't get passed to the index this way:
} else {
$output = 2;
header('Location:index.php');
}
Your options (short of redesigning the way it works) are POST or GET;
} else {
$output = 2;
header('Location:index.php?sent=1');
}
Then in your index.php:
<?php
if (isset($_GET['sent']) && $_GET['sent'] == 1) {
echo "<b>Message has been sent</b>";
}
?>

more button need to show after some count

if(count($fetchExperiences) > 0)
{
$i=0;
foreach($fetchExperiences as $fetchExperience)
{
$sqlExperiencedat = $db->createCommand(" code ");
$fetchExperiencesdat = $sqlExperiencedat->queryAll();
$tdyyy=date('YYYY-mm-dd');
if(strtotime($tdyyy)<strtotime($fetchExperience['experience_date']))
{
foreach($fetchExperiencesdat as $fetchExperiencesdatt)
{
if(strtotime($tdyyy)<strtotime($fetchExperiencesdatt['avail_date']))
{
//echo $fetchExperiencesdatt['avail_date'];
$tth=1;
}
}
if($tth==1){
//fetchExperiencesdatt code
}
}
}
if(count($fetchCountExperiences) > 8){
<a class="more_experience" style="clear:both;"><span onclick="getMoreExperiences1('<? php echo count($userResults);?>','<?php echo $_SESSION['user_id']; ?>','8')">More Experiences</span></a>
}
}
}
My question is that I want more button after $fetchexperiences count is greater than 8. It is fine. But in between $fetchexperiences loop there is a loop for $fetchExperiencesdat. by the loop if there is no experince with in availdate it is not shown. so the probelm is more button is display not after 8 because of some $fetchExperiencesdat elements should be hidden. So please give me any solution for this.
Thanking you.
I'm not sure I understand where your count experiences are coming from. However, the problem your code has is that fetchCountExperiences is not given a value.
if(count($fetchCountExperiences) > 8){
<a class="more_experience" style="clear:both;"><span onclick="getMoreExperiences1('<? php echo count($userResults);?>','<?php echo $_SESSION['user_id']; ?>','8')">More Experiences</span></a>
}
Looks like it should be
if(count($fetchExperiences) > 8){
<a class="more_experience" style="clear:both;"><span onclick="getMoreExperiences1('<? php echo count($userResults);?>','<?php echo $_SESSION['user_id']; ?>','8')">More Experiences</span></a>
}

Categories