简体   繁体   English

PHP不存储会话ID

[英]PHP not storing session ID

I have been silently following along in here and now I joined myself. 我一直默默地跟随着这里,现在我加入了自己。

I am in the making of a login system, purely for the learning of it, as i am semi-new to PHP (~2 months best guess). 我正在开发一个登录系统,纯粹是为了学习它,因为我是PHP的新手(大约2个月的最佳猜测)。

Right now i get the following error (using WAMP and mysql): 现在我得到以下错误(使用WAMP和MySQL):

"Notice: Undefined property: stdClass::$id in C:\\wamp64\\www\\login\\classes\\user.php on line 44" “注意:第44行的C:\\ wamp64 \\ www \\ login \\ classes \\ user.php中的未定义属性:stdClass :: $ id”

This is the line WAMP is telling: 这是WAMP告诉的行:

            session::put($this->_sessionName, $this->data()->id);

And here is the put(& get) function used: 这是使用的put(&get)函数:

public static function put($name, $value){
    return $_SESSION[$name] = $value;
}

public static function get($name){
    return $_SESSION[$name];}

My Login-system is working up until this login-point, even hash's and everything. 我的登录系统正在运行,直到该登录点为止,包括哈希值和其他所有内容。

I am trying to store the ID of the user, so that when i want to check if it is functioning with the following: 我正在尝试存储用户的ID,以便在我要检查其是否与以下功能一起使用时:

echo session::get(config::get('session/session_name'));

after doing this in my user class: 在用户类中执行此操作后:

$this->_sessionName = config::get('session/session_name');

It should just give me the number of the ID on the user (eg 2, if the user ID is 2(ID is auto incremented)). 它应该只给我用户的ID号(例如,如果用户ID为2(ID是自动递增的),则为2)。 (the config holds basic arrays for DB-connection, session and token etc.) (该配置包含用于数据库连接,会话和令牌等的基本数组。)

But it is not. 但事实并非如此。

I really hope this is not a duplicate question as i could not find a question that asked for this kind of use. 我真的希望这不是重复的问题,因为我找不到要求这种使用的问题。

Is there anybody who can help me in the right direction? 有谁能在正确的方向上帮助我?

well error is self explanatory 井差是不言自明的

$this->data() in user.php returns stdClass without id user.php $this->data()返回没有id stdClass

this is matter of simple debugging see what $this->data() returns... 这是简单的调试问题,请参阅$this->data()返回什么...

i will suggest you to used a session class with static member function so that you can call that method from any where with its class name 我建议您使用带有静态成员函数的会话类,以便可以从任何位置使用其类名调用该方法

and most importantly use Error Control Operator when ur going start a session.so that if session is going on, system won't start again and error will ignore so u can ensure about session variable 最重要的是在您开始会话时使用错误控制运算符 。因此,如果会话正在进行,系统将不会再次启动,错误将被忽略,因此您可以确保会话变量

eg:- 例如:-

<?php

class Session
{

    public static function init()
    {  
        @session_start(); //#
    }

    public static function set($key, $value)
    {
        $_SESSION[$key] = $value;
    }

    public static function get($key)
    {

        if (isset($_SESSION[$key]))
            return $_SESSION[$key];
        else 
            return false;
    }

}

now use it like below:- 现在像下面这样使用它:

    <?php  
     require 'Session.php'; 
 class TestController{ 
    function setData()
    {
        Session::init(); 
        Session::set('user_id',1);//set userid to session
    }

    function getData()
    {
        Session::init(); //this also won't give you error;
        echo Session::get('user_id'); //get or retrieve userid from session
    } 
} 
?>

but use Session::init in constructor for good programming practice .. 但是在构造函数中使用Session :: init可以实现良好的编程习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM