Display user ID in PHP URL

Asked

Viewed 411 times

0

Hello, I’m trying to make the user ID appear in the url when they log in. I made a function that receives the login and password of the user and returns the ID. However, when the user logs in, the ID that shows in the URL is always '0'. Independent of users who log in.

index php.

    <form method="POST" action="conteudo.php" name="logar">
        <fieldset>
            <label>
                <input type="text" name="usuario" placeholder="LOGIN"/>
            </label>
            <br><br>
            <label>
                <input type="password" name="pass" placeholder="SENHA"/>
            </label><br><br>
            <input type="submit" name="enviar" value="ACESSAR"/>
            <input type="submit" name="cadastrar" value="CADASTRAR"/>
        </fieldset>
    </form>

php content.

if($_POST['enviar']){
    $login = $_POST['usuario'];
    $senha = sha1($_POST['pass']);

    if(verificaUsuario($conexao,$login,$senha)){
        $id = pegandoID($conexao,$login,$senha);
        header("Location: principal.php?id={$id}");PASSANDO ID PARA URL AQUI

    }elseif(!verificaUsuario($conexao,$login,$senha)){
        header("Location:index1.php?erro=#");
    }
}elseif($_POST['cadastrar']){
        header("Location: cadastro.php");
}

php bank.

include "config/config.php";

try{
    $conexao = new PDO("mysql:host={$servidor};port=3306;dbname={$banco}",$usuario,$senha);
}catch(PDOException $error){
    echo "Erro: " . $error->getMessage() . "<br>";
    die();
}function pegandoID($conexao,$login,$senha){
    $sql = $conexao->exec("SELECT id FROM usuarios WHERE login='$login' AND senha='$senha'");
    return $sql;}

  • The method exec PDO will not return the id selected; instead, use the query.

  • I changed it, but then the error Catchable fatal error appears: Object of class Pdostatement could not be converted to string in C: xampp htdocs php phpoo Testes conteudo.php on line 11. Line 11, is the line of the first header

  • 2

    It is not just a text change, so I have Linked the respective documentation. Read each one and understand how it works.

  • I could not resolve the error. The ID 0 continues to appear in the url.

1 answer

0


Try it this way

include "config/config.php";

try{
    $conexao = new PDO("mysql:host={$servidor};port=3306;dbname={$banco}",$usuario,$senha);
}catch(PDOException $error){
    echo "Erro: " . $error->getMessage() . "<br>";
    die();
}

function pegandoID($conexao,$login,$senha){
    $sql = $conexao->query("SELECT id FROM usuarios WHERE login='$login' AND senha='$senha'")->fetch(PDO::FETCH_ASSOC);
    /*Aqui retorna um array, e sabemos que será apenas uma linha.
      Então trata ele e retorna so o id que você precisa.*/
    return $sql['id'];
}

Suggestion:

->sha1 - It would be better if you used crypt(), is safer, would risk until it would not be possible to roll a Decrypt on it.

->config/config.php - Try not to leave variables with database config in php, you could have a reference file just to fetch these values, such as an xml, or using lib q the Laravel uses phpdotenv

  • I made the modification, but it shows an error now. Fatal error: Cannot use Object of type Pdostatement as array in C: xampp htdocs php phpoo Tests database.php on line 32. Line 32 is the one that contains select to get the ID.

  • Wheel again, missed fetch, which ends up returning directly the result.

  • Thanks Luiz, it worked. Thank you very much.

Browser other questions tagged

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