How do logical operators work in PHP and how to add a GET and POST method differing?

1

Before asking this question I researched a lot, but I could not solve it myself. I'm new to PHP and programming, I wanted to understand how the IF and ELSE operators work in PHP and also the GET and POST methods, what I need to do is to make this code work, but I tried everything and an error: 500 I believe it is by the wrong code). I need two functions that I can not implement, one is the function that if someone tries to save in my bots.txt file an empty text or spaces apprehensible an error; The other is that my restart button does not work.

  

I tested the restart function on a separate file and it worked, but when I joined the error.

Example: Site

<?php 
if(!empty($_POST['text'])){
    $fp = fopen("C:/Users/Administrator/OneDrive/COUP_Server4Edit/bots.txt" , "r");    
    $lines = '';
        while(!feof($fp)){
           $lines .= fgets($fp , 4096) ;               
        }       
    fclose($fp);
    $re = fopen("C:/Users/Administrator/OneDrive/COUP_Server4Edit/bots.txt" , 'w');  
    $lines .= $_POST['text'];       
    $write = fwrite($re , $lines . PHP_EOL);  
    if(empty($write) OR !strstr($write,''))
{
    echo "<script> alert('Não é permitido nomes em branco ou vazios!') </script>"; 
}
    else if($write){
        echo "<script> alert('Bot salvo com sucesso!') </script>";
  }else if($write == false ){
      echo "<script> alert('Ops.. ocorreu um erro inesperado :(')</script>";
  }
}
 if (!empty($_GET['rr'])) {
      echo "<script> alert('Servidor reiniciado com sucesso!') </script>";
exec('wscript "C:/Users/Administrator/OneDrive/COUP_Server4Edit/A_Fechar_Servidor_Hidden.vbs"');
  } else {
      "<script> alert('Erro ao tentar reiniciar!') </script>";
  }

echo '<form action="#" method="post">';
echo '<fieldset>'; 
echo '<legend>Erko Now | COUP - BR</legend>';
echo '<center>';
echo '<img src="img/coupbr.jpg" alt="Coup BR"><br>';
echo '<b>';
echo 'Nome do Bot:'; 
echo '</b>';
echo '<input type="text" name="text" rows="20" cols="50" placeholder="Nome" title="Digite o nome do bot que deseja adicionar">';
echo '<input type="submit" value="Salvar" title="Gravar informações">';
echo '<form action="#" method="get">';
echo '<input type="hidden" name="rr" value="run">';
echo '<input type="submit" value="Reiniciar">';
echo '</form>'; 
echo '</fieldset>'; 
echo '</center>';
echo '</form>';
?>

Below the well-defined code

<?php
if(!empty($_POST['text'])){

    $fp = fopen("C:/Users/Administrator/OneDrive/COUP_Server4Edit/bots.txt" , "r");    
    $lines = '';
        while(!feof($fp)){
           $lines .= fgets($fp , 4096) ;               
        }       
    fclose($fp);
    $re = fopen("C:/Users/Administrator/OneDrive/COUP_Server4Edit/bots.txt" , 'w');  
    $lines .= $_POST['text'];       
    $write = fwrite($re , $lines . PHP_EOL);       

    if(empty($write) OR !strstr($write,'')){
        echo "<script> alert('Não é permitido nomes em branco ou vazios!') </script>"; 
    }else if($write){
            echo "<script> alert('Bot salvo com sucesso!') </script>";
    }else if($write == false ){
          echo "<script> alert('Ops.. ocorreu um erro inesperado :(')</script>";
    }

}

if (!empty($_GET['rr'])) {
    echo "<script> alert('Servidor reiniciado com sucesso!') </script>";
exec('wscript "C:/Users/Administrator/OneDrive/COUP_Server4Edit/A_Fechar_Servidor_Hidden.vbs"');
} else {
    "<script> alert('Erro ao tentar reiniciar!') </script>";
}

echo '<form action="#" method="post">';
echo '<fieldset>'; 
echo '<legend>Erko Now | COUP - BR</legend>';
echo '<center>';
echo '<img src="img/coupbr.jpg" alt="Coup BR"><br>';
echo '<b>';
echo 'Nome do Bot:'; 
echo '</b>';
echo '<input type="text" name="text" rows="20" cols="50" placeholder="Nome" title="Digite o nome do bot que deseja adicionar">';
echo '<input type="submit" value="Salvar" title="Gravar informações">';
echo '<form action="#" method="get">';
echo '<input type="hidden" name="rr" value="run">';
echo '<input type="submit" value="Reiniciar">';
echo '</form>'; 
echo '</fieldset>'; 
echo '</center>';
echo '</form>';
?>
    
asked by anonymous 18.04.2018 / 10:42

1 answer

2

Your code has several problems!

  • One form inside the other does not work correctly. You have to put each other out or try to reply of this post
  • These conditionals

    if(!empty($_POST['text'])){ 
     ..........
     ..........
         if(empty($write) OR !strstr($write,'')){
    

    I would use

    if(isset($_POST['text'])){
    .................
    .................
    $write = trim($_POST['text']); 
    
    if(empty($write)){
        echo "<script> alert('Não é permitido nomes em branco ou vazios!') </script>"; 
    
  • In this line $_POST['text'] is missing $write

  • Consider the comment by Andrei Coelho

    • This "C: / Users / Administrator / OneDrive / .." paths will not work if your site is in a hosting. And depending on the server configuration it forwards direct error 500, 404, and hides the errors of php, mysql, etc.
  • 18.04.2018 / 15:06