sessies-beheer

Gesponsorde koppelingen

PHP script bestanden

  1. sessies-beheer

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
<?php
/**
 * Session control for "van der Berg CMS Framework"
 *
 * @name      van der Berg CMS Framework
 * @version   1.0.4
 * @author    iltar van der berg
 * @copyright free to use if comments remain intact
 * @since     23-12-2007
 *
 * releases
 *
 * 1.0.4 - 28.12.2007
 * - session_control::is_logged_in() now also checks if session_control::ip is equal to $_SERVER['REMOTE_ADDR']
 *   to prevent session hijacking and stealing
 * - added documentation for session_control::get_ip()
 * - added function session_control::get_user_id
 * - added public static $disable_sessions_start
 *   this will ignore the session_start() within the class default is false
 * - added public static $disable_set_session_in_destruct
 *   with this off, the sessions wil not be automaticly updated at __destruct
 * - added function session_control::__destruct()
 *
 * 1.0.3 - 27.12.2007
 * - changed session_control::set_flood_date() date() to time()
 * - added function session_control::get_flood_date()
 * - added function session_control::get_ip()
 * - added function session_control::get_attempt()
 *
 * 1.0.2 - 26.12.2007
 * - changed the handling of returns
 * - session_control::get_custom() now accepts an array for input. Note that if you search for 1,
 *   or only 1 is found, the return will NOT be an array
 * - session::unset_custom() array handling has been changed to speed up things a little
 *
 * 1.0.1 - 25.12.2007
 * - fixed a bug returning an empty value in session_control::set_custom() when
 *   forgot to enter 'type_of_var|' in front of the string. It will now be str(string) standard
 *
 * 1.0.0 - 23.12.2007
 * - initial release
 */

class session_control
{
    /**
     * all available sessions (all declared private)
     *
     * @var $user_id
     * @var $ip
     * @var $logged_in
     * @var $login_attempts
     * @var $register_attempts
     * @var $last_location_cache
     * @var $last_post_date
     * @var $last_reply_date
     * @var $custom_sessions
     */

    private $user_id;
    //private $session_id;          // not yet implemented
    private $ip;
    private $logged_in;
    private $login_attempts;
    private $register_attempts;
    private $last_location_cache;
    private $last_post_date;
    private $last_reply_date;
    private $custom_sessions;
    
    /**
     * set this to false before creating the object to prevent session_start(), use this if you already have session_start() somwhere
     *
     * @var bool
     */

    public static $disable_session_start = false;
    
    /**
     * set this to true to do session_control::set_session() in session_control::__destruct()
     *
     * @var bool
     */

    public static $disable_set_session_in_destruct = false;
    
    /**
     * analyse the sessions and put them into the class
     *
     * @return void
     */

    public function __construct()
    {

        // analyse the sessions and put them in the class
        if(!self::$disable_session_start)
        {

            session_start();
        }

        
        $this->user_id             = isset($_SESSION['user_id'])             ? $_SESSION['user_id']             : (int)0;
        //$this->session_id          = isset($_SESSION['session_id'])          ? $_SESSION['session_id']          : (string)''; not yet implemented
        $this->ip                  = isset($_SESSION['ip'])                  ? $_SESSION['ip']                  : (string)'';
        $this->logged_in           = isset($_SESSION['logged_in'])           ? $_SESSION['logged_in']           : (bool)false;
        $this->login_attempts      = isset($_SESSION['login_attempts'])      ? $_SESSION['login_attempts']      : (int)0;
        $this->register_attempts   = isset($_SESSION['register_attempts'])   ? $_SESSION['register_attempts']   : (int)0;
        $this->last_location_cache = isset($_SESSION['last_location_cache']) ? $_SESSION['last_location_cache'] : (string)'index.php';
        $this->last_post_date      = isset($_SESSION['last_post_date'])      ? $_SESSION['last_post_date']      : (string)'';
        $this->last_reply_date     = isset($_SESSION['last_reply_date'])     ? $_SESSION['last_reply_date']     : (string)'';
        $this->custom_sessions     = isset($_SESSION['custom_sessions'])     ? $_SESSION['custom_sessions']     : array();
    }

    
    /**
     * if the user is logged in, return the ip address
     *
     * @return mixed
     */

    public function get_ip()
    {

        // check if user is logged in, if so, return the ip logged in from
        if($this->logged_in)
        {

            $return = $this->ip;
        }

        else
        {
            $return = null;
        }

        
        return $return;
    }

    
    /**
     * if the user is logged in, return the user_id
     *
     * @return mixed
     */

    public function get_user_id()
    {

        // check if user is logged in, if so, return the user_id
        if($this->logged_in)
        {

            $return = $this->user_id;
        }

        else
        {
            $return = null;
        }

        
        return $return;
    }

    
    /**
     * Check if the user is logged in
     *
     * @return bool
     */

    public function is_logged_in()
    {

        if
        (
            // check user_id
            !is_numeric($this->user_id) || $this->user_id == 0 || trim($this->user_id) == '' ||
            // check session_id
            //!is_string($this->session_id) || empty(trim($this->session_id)) || // not yet implemented
            // check ip

            !is_string($this->ip) || trim($this->ip) == '' || $this->ip != $_SERVER['REMOTE_ADDR'] ||
            // check logged_in
            !is_bool($this->logged_in) || trim($this->logged_in) == '' || $this->logged_in === false
        )
        {

            $this->unset_login();
            $return_this = null;
        }

        else
        {
            $return_this = true;
        }

        
        return $return_this;
    }

    
    /**
     * create login session
     *
     * @param int $user_id
     * @param string[optional] $ip
     * @return bool
     */

    public function set_login($user_id, $ip = null)
    {

        // set the login with the selected user id
        $this->logged_in = (bool)true;
        $this->user_id   = (int)$user_id;
        if(is_null($ip))
        {

            $this->ip = (string)$_SERVER['REMOTE_ADDR'];
        }

        else
        {
            $this->ip = (string)$ip;
        }

        
        return true;
    }

    
    /**
     * empty the login
     *
     * @return bool
     */

    public function unset_login()
    {

        $this->logged_in = (bool)false;
        $this->user_id   = (int)0;
        $this->ip        = (string)'';
        
        return true;
    }

    
    /**
     * input the location into the cach session
     *
     * @param string[optional] $location
     * @param mixed[optional] $query_string
     * @return bool
     */

    public function set_location_cache($location = null, $query_string = null)
    {

        global $root_path;
        
        $qs_array = array();
        
        // check if the page has valid signs and exists, else redirect to index.php
        if(trim($location) == '' || !preg_match('#(\./([a-z]*[\-_][a-z]*/)*){0,1}([a-z]*[\.\-_][a-z]*)*\.php#i', $location) || !file_exists($root_path . $location))
        {

            $location = 'index.php';
        }

        // the file is valid and exists, now it's possible that the file uses a query string
        // check if the query string is valid

        elseif(is_array($query_string))
        {

            foreach($query_string as $key => $value)
            {

                if(!is_array($value) && preg_match('#[a-z][a-z0-9_]*#i', $value) && preg_match('#[a-z][a-z0-9_]*#i', $key) && $key != 'redir')
                {

                    $qs_array[] = $key . '=' . $value;
                }
            }

            
            // create a location with the query_string in it xhtml valid
            $location .= '?' . implode('&amp;', $qs_array);
        }

        
        $this->last_location_cache = $location;
        return true;
    }

    
    /**
     * set flood date moment it was posted, no params for reply and post. else set 'post' or 'reply'
     *
     * @param string[optional] $type_of_post
     * @return bool
     */

    public function set_flood_date($type_of_post = null)
    {

        // set the time in the selected session
        $date = time();
        
        switch($type_of_post)
        {
            case
'post':
                $this->last_post_date = $date;
                break;
            case
'reply':
                $this->last_reply_date = $date;
                break;
            default:

                $this->last_post_date = $this->last_reply_date = $date;
                break;
        }

        
        return true;
    }

    
    /**
     * get the flood date
     * type of post is either post or reply
     * time period is the compare time in seconds
     * return type is either boolean, seconds or session_time
     * boolean will return if the time period has been passed
     * seconds will return the remaining seconds
     * session_time will return the time when it was set with time()
     *
     * @param string $type_of_post
     * @param int[optional] $time_period
     * @param int[optional] $return_type
     * @return mixed
     */

    public function get_flood_date($type_of_post, $time_period = 300, $return_type = 'bool')
    {

        // determine the session that is used
        switch($type_of_post)
        {
            case
'post':
                $type = $this->last_post_date;
                break;
            case
'reply':
                $type = $this->last_reply_date;
                break;
            default:

                $type = time();
                break;
        }

        
        // check what the user wants to return
        // bool is true if the difference is more than $time_period
        // seconds/int returns the difference in seconds between now, and when the session was set
        // session_time returns the time of the session

        switch($return_type)
        {
            case
'boolean':
            case
'bool':
                if((int)(time() - $type) >= (int)$time_period)
                {

                    $return = true;
                }

                else
                {
                    $return = null;
                }

                break;
            case
'sec':
            case
'second':
            case
'seconds':
            case
'int':
                if((int)(time() - $type) >= 0)
                {

                    $return = (time() - $type);
                }

                else
                {
                    $return = null;
                }

                break;
            case
'session':
            case
'time':
            case
'session_time':
            default:

                $return = $type;
                break;
        }

        
        return $return;
    }

    
    /**
     * either increment, $this->login_attempts, $this->register_attempts or reset them to 0
     *
     * @param string $type_of_increment
     * @return bool
     */

    public function increment_attempt($type_of_increment = 'reset')
    {

        switch($type_of_increment)
        {
            case
'login':
                $this->login_attempts++;
                break;
            case
'register':
                $this->register_attempts++;    
                break;
            case
'reset':
            default:

                $this->login_attempts = (int)0;
                $this->register_attempts = (int)0;
                break;
        }

        
        return true;
    }

    
    /**
     * get the login or register attempts
     *
     * @param string $type_of_attempt
     * @return int
     */

    public function get_attempt($type_of_attempt)
    {

        switch($type_of_attempt)
        {
            case
'login':
                $return = (int)$this->login_attempts;
                break;
            case
'register':
                $return = (int)$this->register_attempts;    
                break;
            default:

                $return = (int)0;
                break;
        }

        
        return $return;
    }

    
    /**
     * create or update custom sessions eg 'session_key' => 'string|this is a string.'
     * please note that an array will overwrite the old one, use get_custom first to
     * merge te arrays, for arrays, resource and objects, please use $obj->set_custom($session_setup, '', false);
     *
     * possible entries in front of the explode character
     * int/integer
     * bool/boolean
     * empty/null
     * obj/object
     * array
     * resource
     * str/string
     * resource - default
     *
     * @param array $session_setup
     * @param string[optional] $explode_this_character
     * @param bool[optional] $check_for_explode_in_value
     * @return bool
     */

    public function set_custom($session_setup, $explode_this_character = '|', $check_for_explode_in_value = true)
    {

        // check if it is an array
        if(is_array($session_setup))
        {

            // create a loop for each input session
            foreach($session_setup as $key => $value)
            {

                // check if explode is needed
                if($check_for_explode_in_value)
                {

                    // check whether the explode character is found or not, if not, add str|
                    if(!strpos($value, $explode_this_character))
                    {

                        $value = 'str|' . $value;
                    }

                    
                    // explode the value
                    $exploded = explode($explode_this_character, $value, 2);
                    
                    $exploded[0] = trim($exploded[0]);
                    
                    switch($exploded[0])
                    {

                        // it's an int
                        case 'integer':
                        case
'int':
                            $session_setup[$key] = (int)$exploded[1];
                            break;
                            
                        // it's a bool
                        case 'boolean':
                        case
'bool':
                            $session_setup[$key] = (bool)$exploded[1];
                            break;
                            
                        // it's empty
                        case 'empty':
                        case
'null':
                            $session_setup[$key] = null;
                            break;
                            
                        // it's an object
                        case 'object':
                        case
'obj':
                            $session_setup[$key] = (object)$exploded[1];
                            break;
                            
                        // it's an array
                        case 'array':
                            $session_setup[$key] = array();
                            $session_setup[$key] = $exploded[1];
                            break;
                            
                        // it's a resource
                        case 'resource':
                            $session_setup[$key] = $exploded[1];
                            break;
                        // it's a string
                        case 'string':
                        case
'str':
                            $session_setup[$key] = (string)$exploded[1];
                            break;
                        default:

                            $session_setup[$key] = $exploded[1];
                        break;
                    }
                }
            }

            
            // return the array, first the old one, than overwrite same values by the new one
            $this->custom_sessions = array_merge($this->custom_sessions, $session_setup);
            
            $return_this = true;
        }

        else
        {
            $return_this = null;
        }

        
        return $return_this;
    }

    
    /**
     * remove the custom session eg "array('foo', 'bar')" will remove "foo" and "bar", "foo" will remove "foo"
     *
     * @param mixed $remove_this
     * @return bool
     */

    public function unset_custom($remove_this)
    {

        // check if input is array or not, if not, convert it inot one
        if(!is_array($remove_this))
        {

            $remove_this = array($remove_this);
        }

        
        // create a loop $value is the key of the custom_sessions
        foreach($remove_this as $key => $value)
        {

            if(array_key_exists($value, $this->custom_sessions))
            {

                $this->custom_sessions[$value] = null;
                unset($this->custom_sessions[$value]);
            }
        }

        
        return true;
    }

    
    /**
     * get one array item back from the custom sessions
     *
     * @param mixed $search_for
     * @return mixed
     */

    public function get_custom($search_for)
    {
    
        // if it is not a string, integer or array, return null
        if(!is_string($search_for) && !is_int(!$search_for) && !is_array($search_for))
        {

            return null;
        }

        
        // make it an array if it is not already
        if(!is_array($search_for))
        {

            $search_for = array($search_for);
        }

        
        // walk the array and check if the sessions exist
        foreach($search_for as $key => $value)
        {

            // if it exist add it to an array
            if(isset($this->custom_sessions[$value]))
            {

                $custom_sessions[] = $this->custom_sessions[$value];
            }
        }

        
        // check what to return, case 0: returns null, case 1: return (string)first array item, default: return the compleet result array
        switch(count($custom_sessions))
        {
            case
1:
                $return_this = (string)$custom_sessions[0];
                break;
            case
0:
                $return_this = null;
                break;
            default:

                $return_this = $custom_sessions;    
        }

        
        return $return_this;
    }

    
    /**
     * return the values to the sessions
     *
     * @return bool
     */

    public function set_sessions()
    {

        $_SESSION['user_id']             = $this->user_id;
        //$_SESSION['session_id']          = $this->session_id; not yet implemented
        $_SESSION['ip']                  = $this->ip;
        $_SESSION['logged_in']           = $this->logged_in;
        $_SESSION['login_attempts']      = $this->login_attempts;
        $_SESSION['register_attempts']   = $this->register_attempts;
        $_SESSION['last_location_cache'] = $this->last_location_cache;
        $_SESSION['last_post_date']      = $this->last_post_date;
        $_SESSION['last_reply_date']     = $this->last_reply_date;
        $_SESSION['custom_sessions']     = $this->custom_sessions;
        
        return true;
    }

    
    /**
     * automaticly set the sessions if $disable_set_session_in_destruct is false
     *
     * @return void
     */

    public function __destruct()
    {

        if(!self::$disable_set_session_in_destruct)
        {

            $this->set_sessions();
        }
    }
}


?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.