Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    Problema con PDO e login

    sto cercando di fare un sistema di login con PDO.
    ho aggiunto questo metodo alla classe:
    Codice PHP:
        public function login($user$pass) {
            
    $pass sha1($pass);
            
    $strQuery "SELECT * FROM user WHERE username='" $user "' AND user_pass='" $pass "'";
            try {
                
    $query $this->pdo->query($strQuery);
                if (
    $query->fetchColumn() == 1) {
                    
    $_SESSION['login_eseguito'] = TRUE;
                    
    $_SESSION['user_id'] = $result->user_id;
                    return 
    TRUE;
                } else {
                    return 
    FALSE;
                }
            } catch (
    PDOException $e) {
                print 
    "Error!: " $e->getMessage() . "
    "
    ;
                die();
            }
        } 
    questa la pagina di login:
    Codice PHP:
    <?php
    session_start
    ();
    include_once 
    'config.php';
    $obj = new Config();
    if (
    $obj->verifica_sessione()) {
        
    header("location:index.php");
    }
    if (
    $_SERVER["REQUEST_METHOD"] == "POST") {
        
    $login $obj->login(htmlentities($_POST['username'], ENT_QUOTES), htmlentities($_POST['password'], ENT_QUOTES));
        if (
    $login) {
            
    header("location:area_riservata.php");
        } else {
            echo 
    'I dati indicati non sono corretti.';
        }
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Login</title>
        </head>
        <body>
            <?php
            
    include_once 'menu.html';
            
    ?>
            <table>
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                    <tbody>
                        <tr>
                            <td>User:</td>
                            <td><input type="text" name="username" value="ogadmin" /></td>
                        </tr>
                        <tr>
                            <td>Password:</td>
                            <td><input type="password" name="password" value="ogferzap" /></td>
                        </tr>
                        <tr>
                            <td><input type="submit" value="Send" /></td>
                        </tr>
                </form>
            </table>
        </body>
    </html>
    quando do invio ottengo sempre questo errore:
    Fatal error: Call to a member function fetchColumn() on a non-object in /web/htdocs/www.orangeneration.it/home/og/email/config.php on line 33
    la riga 33 è quella dove inizia l'if:
    if ($query->fetchColumn() == 1) {

    qualche idea??

  2. #2
    Utente di HTML.it L'avatar di bstefano79
    Registrato dal
    Feb 2004
    Messaggi
    2,520
    il problema è qui, ma non dirti perchè

    $query = $this->pdo->query($strQuery);

    questo non ti restituisce un oggtto come dovrebbe e nel controllo successivo si arrabbia dicendo che non puoi chimare un metodo su una variabile non oggetto

  3. #3
    potrebbe essere un problema di classe allora:
    Codice PHP:
    <?php

    class Config {

        private 
    $pdo;

        public function 
    __construct() {
            try {
                
    $this->pdo = new PDO(......);
            } catch (
    PDOException $e) {
                print 
    "Error!: " $e->getMessage() . "
    "
    ;
                die();
            }
        }

        public function 
    verifica_sessione() {
            if (isset(
    $_SESSION['login_eseguito'])) {
                return 
    $_SESSION['login_eseguito'];
            } else {
                return 
    FALSE;
            }
        }

        public function 
    login($user$pass) {
            
    $pass sha1($pass);
            
    $strQuery "SELECT * FROM user WHERE username='" $user "' AND user_pass='" $pass "'";
            try {
                
    $query $this->pdo->query($strQuery);
                if (
    $query->fetchColumn() == 1) {
                    
    $_SESSION['login_eseguito'] = TRUE;
                    
    $_SESSION['user_id'] = $result->user_id;
                    return 
    TRUE;
                } else {
                    return 
    FALSE;
                }
            } catch (
    PDOException $e) {
                print 
    "Error!: " $e->getMessage() . "
    "
    ;
                die();
            }
        }

        public function 
    select() {
            
    $result = array();
            
    $strQuery "SELECT * FROM contatti";
            try {
                
    $result $this->pdo->query($strQuery);
                return 
    $result;
            } catch (
    PDOException $e) {
                print 
    "Error!: " $e->getMessage() . "
    "
    ;
                die();
            }
        }
    .......
    però mi pare strano perchè il metodo select() funziona bene.

  4. #4
    Utente di HTML.it L'avatar di bstefano79
    Registrato dal
    Feb 2004
    Messaggi
    2,520
    infatti a me sembra tutto corretto, l'unica cosa potrebbe essere la query sbagliata hai provato ad eseguirla in phpmyqdmin?

  5. #5
    apportate queste modifiche funziona:
    Codice PHP:
        public function login($user$pass) {
            
    $pass sha1($pass);
            
    $strQuery "SELECT * FROM user WHERE user_name='" $user "' AND user_pass='" $pass "'";
            try {
                
    $query $this->pdo->prepare($strQuery);
                
    $query->execute();
                if (
    $query->rowCount() == 1) {
                    
    $_SESSION['login_eseguito'] = TRUE;
                    
    $_SESSION['user_id'] = $result->user_id;
                    return 
    TRUE;
                } else {
                    return 
    FALSE;
                }
            } catch (
    PDOException $e) {
                print 
    "Error!: " $e->getMessage() . "
    "
    ;
                die();
            }
        } 
    ho usato un'istruzione preparata in pratica.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.