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

Asked

Viewed 52 times

0

Before I asked that question, I did a lot of research, but I couldn’t figure it out on my own. Well I’m new in PHP and also in 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 make this code work but I’ve tried everything and a Error:500 (I think it’s the incorrect code). I need two functions that I can’t implement, one is the function that if someone tries to save in my bots.txt file an empty text or spaces shows an error; the other is that my reset button doesn’t work.

I tested the restart function in a separate file and it worked, but while joining the error.

Example: Website

<?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>';
?>

Follow below the code well identado

<?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>';
?>
  • No problem if you give me sources where I can solve my problem, I search on my own if that’s the case, thank you in advance.

  • 2

    This problem can be found more easily with the code well identado, gets the tip

  • 2

    These paths "C:/Users/Administrator/Onedrive/.." will not work if your site is in a hosting. And depending on the server configuration it forwards directly to error 500, 404, and hides the errors of php, mysql, etc..

  • agree with @Andreicoelho because your site should not have permission to access the directory

1 answer

2


Your code has several problems!

  1. One form inside the other does not work correctly. You have to put one outside the other or try as per reply from that post
  2. These conditional

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

    seem to make no sense since $_POST['text'] it has already been verified that it is not empty and logically the variable $write will not be empty.

    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>"; 
    
  3. along those lines "<script> alert('Erro ao tentar reiniciar!') </script>"; missing a echo

  4. Watch out for the comment of Andrei Coelho

    • These "C:/Users/Administrator/Onedrive/." paths will not work if your website is hosted. And depending on the server configuration it forwards directly to error 500, 404, and hides the errors of php, mysql, etc.
  • 1

    Exact. Sometimes people do not do the configuration properly and simply make a deploy thinking that the code will work in production equal to the local functioning. It would be right for him to create a constant and use it in the absolute path of his files.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.