Starting in POO (PHP) and I have the following question;

Asked

Viewed 54 times

0

I’ve been searching for information for a long time to unravel the mysteries of POO (PHP) and I can’t find them.

I would like to know how to call a certain function in another file by running a form, example;

<<login.php>>

 <form name="login" method="post" action="funcoes.php">
        <h1>Acesso</h1>
        <p>Nick
           <input type="text" id="user" placeholder="Insira o nome de usuario" name="user"></p>
        <p>Senha
          <input type="password" id="senha" placeholder="Insira a senha" name="senha"></p>
        <p class="lead">
          <button type="submit">Entrar</button>
        </p>
      </main>
    </form>

<<php functions.>>

 <?php
         class conta{
        
                function autenticacao(){
                    $user=htmlspecialchars($_POST["user"]);
                    $senha=htmlspecialchars($_POST["senha"]);
                    $sql="select * from operador where usuarioOp='$user'";
              
                    $resultado=mysqli_query($link,$sql);
                    $fim=mysqli_fetch_array($resultado);
              
                    if($senha=$resultado){
                        echo"log_ok";
                        $_SESSION['id']=$fim['idOp'];
                        $_SESSION['nick']=$fim['usuarioOp'];
                        $_SESSION['nome']=$fim['nomeOp'];
                        $_SESSION['rank']=$fim['rankOp'];
                      
 
 $_SESSION['nick']=md5('seg'.$_SERVER['REMOTE_ADDR'].$_SERVER['HTTP_USER_AGENT']);
                        
                        header('location:login.php');
                        }else { 
                        echo"log_erro";
                        session_unset();
                        header('location:index.php');
                      }
                }
        
                function cadastro(){
                    $user=htmlspecialchars($_POST["user"]);
                    $senha=htmlspecialchars($_POST["senha"]);
                    $nome=htmlspecialchars($_POST["nome"]);
                    $nasc=htmlspecialchars($_POST["nascimento"]);
                    $cpf=htmlspecialchars($_POST["cpf"]);
                    $rank=htmlspecialchars($_POST["rank"]);
                    $result=mysqli_query($link,"insert into operador (usuarioOp, senhaOp, cpfOp, nomeOp, nascimentoOp, rankOp) values ('$user', '$senha', '$cpf', '$nome', '$nasc', '$rank' )")or die (mysqli_error());
                        if($result){
                            $_SESSION["nome"]=$nome;
                            $_SESSION["user"]=$user;
                            echo "cad_ok";
                            header("Location:online.php");
                            
                        }else {
                              echo "cad_erro";
                              header("Location:login.php")
                              }
                }
            }
        ?>

I did just that and I can not find anything related to my doubt on the internet, I would like to use only PHP to call the function Appliance in the archive php functions. by requesting Ubmit on the login.php. before I was using IF and so on, but I decided to try to find new methods to speed up the procedures.. because staying doing if for each function becomes difficult... a lot of similar function with different data collection.

I thank anyone who can help me. And I hope that this will solve some doubts of others in the future, if it is solved. Big hug.

  • 2

    It is interesting to keep in mind that just using a class does not mean that it is using the POO paradigm. The paradigm has principles that must be followed, such as, but not limited to, abstraction and inheritance. Without it, you just created a class to replace something that could be functions. In fact, the way it is structuring the code, it would make more sense to use the procedural paradigm, with functions, than to take risks in the POO.

  • Hello my friend certainly did not know how to ask me about, is that I imagined that most who knew of POO (PHP) would answer this question with a ease... rs Actually I wanted to perform even functions, but I was not successful. If you can/know methods to help me, I’ve been searching for ways to do this for a long time, I read and reread the PHP documentation and I couldn’t do it. Hug.

1 answer

0

You can use (new conta())->autenticacao() at the end of the class declaration. To avoid ifs you can create more files, for example autenticacao.php containing:

require_once 'func.php';
(new conta())->autenticacao();

And another called cadastro.php:

require_once 'func.php';
(new conta())->cadastro();

The action of the form is:

<form name="login" method="post" action="autenticacao.php">

Still, try using some framework (Laravel, symfony, etc) to standardize your code.

  • Hello! Thanks for the help! I was in doubt about how I will call exactly the function, since it is all variables outside the class, right? Then when I call gives error in the empty variables, and the screen ends up being blank...

Browser other questions tagged

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