Ver Mensaje Individual
  #1 (permalink)  
Antiguo 03/11/2012, 14:31
Avatar de necoide
necoide
 
Fecha de Ingreso: diciembre-2011
Mensajes: 9
Antigüedad: 12 años, 4 meses
Puntos: 1
Problema: PHP PDO y $_SESSIONS

index.php
Código PHP:
<?php 
    session_start
();
    include(
'Core/book.php');
?>
<?php 
if (!isset($_SESSION['username'])): header("Location: login.php"); ?>

<?php else: ?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <link rel="StyleSheet" href="Styles/styles.css" type="text/css">
    </head>
    <body>

        <h2> <?php echo $_SESSION['username']; ?> </h2>
        <?php
            $topic 
= new Topics();
            
$topic->GetTopics();
        
?>

        <a href="newtopic.php">Create Topic<a/><br />
        <a href="logout.php">Logout</a>
    
    </body>
</html>    
<?php endif; ?>
user.php
Código PHP:
<?php
class User
{
    private 
$conex;
    private 
$Name;
    private 
$Password;
    private 
$Id;
    
    public function 
__construct($username$password)
    {
        
$this->Name $username;
        
$this->Password $password;
        
$this->Connect();
    }
    
    public function 
__wakeup()
    {
        
$this->Connect();
    }
    
    private function 
Connect()
    {
        
$servername $_SERVER['SERVER_NAME'];
        if (
$servername "Localhost")
        {
            
$this->conex = new PDO('mysql:host=localhost;dbname=SistemaDeUsuarios''root''');
            
//$this->conex = new mysqli('localhost','root','','SistemaDeUsuarios');
        
}
        
    }
    
    public function 
Name() {
        return 
$this->Name;
    }
    public function 
Id() {
        return 
$this->Id;
    }
    
    public function 
Register()
    {
        
$ok $this->conex->query("INSERT INTO users(username,password) VALUES('$this->Name','$this->Password')"
                or die(
$this->conex->error);
        if (
$ok) return 1;
        return 
0;
    }
    public function 
Exist()
    {
        
$sql "SELECT username,userid FROM users WHERE username=:username and password=:password LIMIT 0,1";
        
$stmt $this->conex->prepare($sql);
        
$stmt->execute(array(':username' => $this->Name':password' => $this->Password));
        
        
$result $stmt->fetch();
        
        if (
$result)
        {
            
$this->Id $result['userid'];
            return 
1;
        }
        return 
0;
        
//        $result = $this->conex->query(" SELECT username,userid FROM users WHERE username='$this->Name' and password='$this->Password' LIMIT 0,1 ");
//        if ($result) 
//        {
//            if ($result->num_rows > 0)
//            {
//                $r = $result->fetch_assoc();
//                $this->Id = $r['userid'];
//                return 1;
//            }
//        }
//        return 0;
    
}
    public function 
NewComment($comment$topicid)
    {
        
$ok $this->conex->query("INSERT INTO comments(comment,topicid,userid,date) VALUES('$comment','$topicid','$this->Id',NOW())"
                or die(
$this->conex->error);
        if (
$ok) return 1;
        return 
0
    }
}
?>
login.php

Código PHP:
<?php
    
require('Core/user.php');
    if (isset(
$_REQUEST['user']) && isset($_REQUEST['password']))
    {
        
$user = new User($_REQUEST['user'], $_REQUEST['password']);
        if (
$user->Exist())
        {
            
session_start();
            
$_SESSION['username'] = $user->Name();
            
$_SESSION['userid'] = $user->Id();
            
$_SESSION['user'] = $user;
            
header("Location: index.php");
        } else {
            
$error "Login Error";
        }
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <link rel="StyleSheet" href="Styles/styles.css" type="text/css">
        <script type="text/javascript">
            function SetFocus() {
                document.getElementById('userinput').focus();
            }
        </script>
    </head>
    <body onload="SetFocus()" style="font-family: Consolas;">
        <div id="form">
            <p>You must be logged in</p>
            <form method="post" action="">
                <div id="labels">
                    <label>User:</label>
                    <label>Password:</label>
                </div>
                <div id="inputs">
                    <input name="user" type="text" id="userinput">
                    <input name="password" type="password">
                    <input type="submit" value="Send">
                </div>
                <?php if (isset($error)) echo $error?>
            </form>
        </div>    
        <a href="register.php">Register</a>
    </body>
</html>

PROBLEMA: se pierden los datos de $_SESSION.

En login.php si el user existe ($user->Exist()) cargo en $_SESSION el name y el id y me redirige a index.php
El problema es que cuando en index.php verifica si esta seteado $_SESSION['username'] el array $_SESSION esta en 0, sin valor (eso que tendria que estar cargado por el login). Y como la condicion es falsa vuelvo a login.php y asi infinitamente.
Esto me pasa solamente como uso PDO en Exist() si uso mysqli funciona bien!
No se que es lo que estoy haciendo mal.