if on the screen before the query is performed

Asked

Viewed 52 times

1

I am trying to create a consultant with PHP who makes a request in a form with an email from the client and returns me if he is registered in the service. The problem is that when I start my page directly the if prints on screen the value true (he must only pass me the values of if after the consultation made).

HTML code:

<html>
    <head>
        <!-- Latest compiled and minified CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

        <!-- Optional theme -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

        <!-- Latest compiled and minified JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<head>
</head>
<head>

    <title>.::Cliente Consultor::.</title>
</head>
<body>
<form method="POST">
<center>
    <BR>
        <BR>
            <BR>
<font size="4" color="orange" face="Segoe Print">.::Consulta::.<img src=""  </font></span></p></div align=""> 
<BR>
    <BR>
        <BR>
<input type="text" name="bin">
<input type="submit">
</center>   
</form>
</body>
<style>
body { 
  background: url() no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  color: black;
}
.twitter a{
  text-decoration: none;
  font-family: Arial, sans-serif  ;
  font-size: 50px;
  text-shadow: grey 0px 0px 10px;
}


}</style>
</html>

PHP code:

<?php


 error_reporting(0);

$bin = $_POST['bin'];

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "https://minhaconta.payleven.com.br/forgot-password");
            curl_setopt($ch, CURLOPT_HEADER, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

           curl_setopt( $ch, CURLOPT_POSTFIELDS, "password_token_type[email]=$bin" );
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            $s = curl_exec($ch);

$mensagem = ' ';


if ($1 == $2) 

{



            $q = explode('</title>', $s);  
            $1 = $q[1];
            $2 = $q[1];
            $binchecker = " [".$bin."] ";

$mensagem = 'cliente encontrado';

} 

else

{

$mensagem= 'cliente não cadastrado';

}


   echo "<br><br><center>".$mensagem."</center>";




?>
  • Is the PHP code in the same file as the HTML? And shouldn’t you run the check only when the form is submitted? Then one is missing if validating the request. Who are $1 and $2 and what you should do if ($1 == $2)? Read about the PHP variable naming rules.

  • All the code is in a single index.php file the variables $1 and $2 contain the contents of the explode that stores the html of the page in which the request was made, if compares the two and returns me true or false

  • I practically want to check if an email is registered on this site and return me the email with the answer next

1 answer

1


  1. To execute a PHP code only when the form is submitted, you need to check the HTTP method of the request handled and the fields present in that request.

    <?php
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // Código aqui...
    }
    
  2. Do not use error_reporting(0) without knowing what you’re doing. In my view, you should never use this line of code. This will hide any error message in your code, but error messages are your best friends.

  3. PHP does not allow variables with names starting with numbers, either letter or underlined. Read documentation for more details.

  4. It makes no sense for you to check the value of two variables before you define it. You are setting the value of $1 and $2 only within the if, then in condition $1 == $2 neither exists.

Remembering that $1 and $2 are not valid names for variables.

  1. It doesn’t make sense for you to do the echo of an HTML element outside the tag html and body. The message should be properly displayed in the body of the page.
  • Thank you very much helped me, my code is working perfectly

Browser other questions tagged

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