Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-30-2009, 11:59 PM   PM User | #1
Kev0121
New Coder

 
Join Date: Mar 2009
Posts: 97
Thanks: 10
Thanked 0 Times in 0 Posts
Kev0121 is an unknown quantity at this point
Undefined Property

I'm getting Notice: Undefined property: session::$user_id in C:\wamp\www\blog\session.php on line 19 that error when logging in, heres my session and log script

SESSION :

PHP Code:
<?php

class session {
    
    private 
$logged_in false;
    public 
$user_id;

    function 
__construct() {
        
session_start();
        
$this->check_login();
    }

    public function 
is_logged_in() {
        return 
$this->logged_in;
    }

    public function 
login($user) {
        if(
$user) {
            
$this->user_id $_SESSION['user_id'] = $this->user_id;
            
$this->logged_in true;
        }
    }

    private function 
check_login() {
        if(isset(
$_SESSION['user_id'])) {
            
$this->user_id $_SESSION['user_id'];
            
$this->logged_in true;
        } else {
            unset(
$this->user_id);
            
$this->logged_in false;
        }
    }

}

$session = new Session();

?>
LOGIN :

PHP Code:
<?php
if(isset($_COOKIE['logged_in'])) {
    
header('Location: usercp.php');
}
?>
<html>
    <head>
        <title>Login.</title>
    </head>
<body>
<div id="log_form" align="center">
<form name="login" method="post" action="">
    <table>
        <tr>
            <td>Username: </td>
            <td> <input type="text" name="log_user" /> </td>
        </tr>

        <tr>
            <td>Password: </td>
            <td> <input type="password" name="log_pass" /> </td>
        </tr>

        <tr>
            <td> <input type="submit" name="log" value="Login" /> </td>
        </tr>
    </table>
</form>
</div>
 </body>
</html>

<?php
require_once 'connection.php';
require_once 
'session.php';
require_once 
'user.php';
connect();
$user = new User();

if(isset(
$_POST['log'])) {
   
User::$user trim($_POST['log_user']);
   
User::$pass  $_POST['log_pass'];

    if(!
User::$user) {
        echo 
"Please enter a username. <br>";
    }

    if (!
User::$pass) {
        echo 
"Please enter a password. <br>";
    }

//    if ($user) {
//        $sql = "SELECT * FROM users WHERE username = '$user'";
//        $res = mysql_query($sql);
//        $count = mysql_num_rows($res);
//
//        if($count == 1) {
//            $find_pass = "SELECT * FROM users WHERE username = '$user' AND password = '$pass'";
//            $res1 = mysql_query($find_pass);
//            $check = mysql_num_rows($res1);
//
//            if($check == 1) {
//                $row = mysql_fetch_array($res1);
//                $session->login($check);
//                setcookie("logged_in", "1", time()+3600);
//                $_SESSION['uid'] = $row['id'];
//                $update = mysql_query("UPDATE users SET active = '1' WHERE id = '".$_SESSION['uid']."'");
//                header('Location: usercp.php');
//            }else {
//                echo "Username and password combination is wrong. <br>";
//            }
//        }else {
//        echo "Sorry we cant find that username in the database. <br>";
//    }
//    }

    
    
$find_user User::authenticate(User::$userUser::$user);

    if(
$find_user) {
        
$session->login(User::$user);
    }

}
?>
Kev0121 is offline   Reply With Quote
Old 05-01-2009, 12:06 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
PHP Code:
    private function check_login() { 
        if(isset(
$_SESSION['user_id'])) { 
            
$this->user_id $_SESSION['user_id']; 
            
$this->logged_in true
        } else { 
            unset(
$this->user_id); 
            
$this->logged_in false
        } 
If !isset($_SESSION['userid']) you destroy $this->user_id.
Then in you're login script:
PHP Code:
    if($find_user) { 
        
$session->login(User::$user); 
    } 
chains back to session here:
PHP Code:
   public function login($user) { 
        if(
$user) { 
            
$this->user_id $_SESSION['user_id'] = $this->user_id
            
$this->logged_in true
        } 
    } 
And its toast. Not sure why you're chaining $this->user_id = $_SESSION['user_id'] = $this->user_id. It sorta doesn't make sense to set the same variable twice.

Solution, don't unset you're user_id, but set it to null.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:18 AM.


Advertisement
Log in to turn off these ads.