login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Snippets > session class 1.0

session class 1.0

Auteur: Stijn - 08 augustus 2006 - 23:03 - Gekeurd door: nemesiskoen - Hits: 3298 - Aantal punten: 4.00 (1 stem)



ik heb een session class gemaakt zonder gebruik te maken van de php session functions. Deze maakt een session bestand op je server.

deze functies kan je gebruiken van de class
1/ boolean __construct( $timeout , $timeoutforce ) functie: $session = new Session(300); //na 5 minuten geen actie verwijdert hij de sessie. bij $timeoutforce = true gaat hij alle bestanden controleren ipv 1 bestand. Dit is handig voor oude sessie bestanden te wissen maar hier geld ook bij grote servers duurt het een eindje.

2/ boolean register_var( $name , $value ) functie: maakt een nieuwe sessie variable aan. $session->register_var('test','waarde');

3/ boolean update_var( $name , $value , $replace = 1 , $force = false ) functie ($force wil zeggen als $name niet bestaat in de sessie dat hij hem gewoon registreert) $replace is de methode hoe hij de waarde moet updaten (meer info bij de phpdoc van de functie): $session->update_var('test','waarde', 1 , true);

4/ boolean delete_var( $name ) functie: verwijdert een variable uit de sessie. $session->delete_var('test');

5/ string get_var( $name ) functie: returnt de waarde van de sessie variable. $session->get_var('test');

6/ array get_history( ) functie: returnt the hele sessie geschiedenis in een array.
var_dump( $session->history() );

7/ boolean destroy( ) functie: verwijdert de sessie en al zijn variablen. $session->destroy( );

8/ boolean exists( $name ) functie: controleert of de sessie variable bestaat.
$session->exists('test');


voor meer vragen en info kan je een pm sturen of op me msn adres (stijnleenknegt1@hotmail.com of stijnleenknegt@gmail.com)

note 1: gebruik deze niet voor grote projecten omdat deze nog in beta is en misschien onveilig en nog niet getest is op grote schaal maar hij werkt wel.

note 2: enkel voor php 5 servers

note 3: alle sessie variablen worden gecontroleerd op patroon. alle namen zijn goed maar dit mag NIET:
*3.1: $session->register_var('8test'); -> variable mag niet beginnen met een letter
*3.2: $session->register_var('è!àdju') -> geen speciale tekens om geen problemen te hebben met charsets en dergelijke
*3.3: $session->register_var('test_var_2') -> dit is een voorbeeld van een CORRECTE session var.

note 4: er zit een .htpasswd en .htaccess bestand in de zip file (download)(session map) en wordt ten strengste aangeraden om deze te bewerken met een eenvoudige tekstbewerker (notepad , gedit , etc)

Code:
session.class.php
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4.  * a session class without use of any session functions of the php language
  5.  *
  6.  *
  7.  * @license GNU/GPL http://www.gnu.org/licenses/gpl.html
  8.  * @author stijn1989 <stijnleenknegt@gmail.com>
  9.  * @version Versie 1.0
  10.  * @package Session
  11.  */
  12.  
  13.  
  14. /**
  15.  * The session class
  16.  *
  17.  * @package Session
  18.  * @author stijn1989
  19.  */
  20. class Session
  21. {
  22.  
  23.  
  24. /**
  25. * constantes of the session class
  26. */
  27. const ERROR_SESSION_WRITE_FILE = 'Could not write the session data to the session file!';
  28. const ERROR_SESSION_OPEN_FILE = 'Could not open the session file!';
  29. const ERROR_SESSION_CLOSE_FILE = 'Could not close the session file!';
  30. const ERROR_SESSION_OPEN_DIR = 'Could not open the dir!';
  31. const ERROR_SESSION_READ_DIR = 'Could not read the dir!';
  32. const ERROR_SESSION_CLOSE_DIR = 'Could not close the dir!';
  33. const ERROR_SESSION_READ_FILE = 'Could not read the contents of the session file!';
  34. const ERROR_SESSION_DELETE_FILE = 'Could not delete the session file.';
  35. const ERROR_SESSION_TOUCH_FILE = 'Could not toch the file.';
  36.  
  37.  
  38.  
  39. /**
  40. * the session
  41. *
  42. * @access private
  43. * @name $_session
  44. */
  45. static private $_session = array( );
  46.  
  47.  
  48. /**
  49. * length of the session ID
  50. *
  51. * @access private
  52. * @name $_sidl
  53. */
  54. static private $_sidl = 17;
  55.  
  56.  
  57. /**
  58. * path to store the session files
  59. *
  60. * @access public
  61. * @name $session_path
  62. */
  63. static public $session_path = './session/';
  64.  
  65.  
  66. /**
  67. * strong error handling?
  68. *
  69. * @access public
  70. * @name $session_error_force
  71. */
  72. static public $session_error_force = true;
  73.  
  74.  
  75.  
  76. /**
  77. * construct - start the session
  78. * $timeoutforce = true -- it will check all session files on the server
  79. * servers with lot's of traffice/visitors should put this to false
  80. * if you don't won't long loadtimes and site-errors.
  81. *
  82. * @access public
  83. * @param integer $timeout
  84. * @param boolean $timeout_force
  85. * @return boolean
  86. */
  87. public function __construct( $timeout = 0 , $timeoutforce = false )
  88. {
  89.  
  90. //check if the session isn't already running - update the history of the session
  91. if( $this->session_check_alive() === true ) {
  92. //check the timeout
  93. if( $timeout != 0 && $timeoutforce === false ) {
  94. $this->session_check_timeout();
  95. } elseif( $timeout != 0 && $timeoutforce === true ) {
  96. $this->session_check_timeout_force();
  97. }
  98.  
  99. //update when it isn't timeout
  100. if( $this->session_check_alive() === true ) {
  101. $this->session_update_data_history( time() , $_SERVER['REQUEST_URI'] );
  102. } else {
  103. return false;
  104. }
  105.  
  106. return true;
  107. }
  108.  
  109. //session setup
  110. self::$_session = array( 'id' => $this->create_session_id() , 'ip' => $_SERVER['REMOTE_ADDR'] , 'start' => time() );
  111.  
  112.  
  113. $session = array( 'session_ip' => self::$_session['ip'] ,
  114. 'session_id' => self::$_session['id'] ,
  115. 'session_start' => self::$_session['start'] ,
  116. 'session_timeout' => $timeout ,
  117. 'session_host' => $_SERVER['HTTP_HOST'] ,
  118. 'session_data' => array( 'session_history' => array( time() => $_SERVER['REQUEST_URI'] ) ,
  119. 'session_vars' => array( )
  120. )
  121. );
  122.  
  123. //create the new session
  124. try {
  125.  
  126. if( touch( $this->session_file_name() , time() , time() ) === false ) {
  127. throw new Exception( self::ERROR_SESSION_TOUCH_FILE );
  128. }
  129.  
  130. if( ($fp = fopen( $this->session_file_name() , 'w' )) === false ) {
  131. throw new Exception( self::ERROR_SESSION_OPEN_FILE );
  132. }
  133.  
  134. if( fwrite( $fp , $this->session_data( $session ) ) === false ) {
  135. throw new Exception( self::ERROR_SESSION_WRITE_FILE );
  136. }
  137.  
  138. if( fclose( $fp ) === false ) {
  139. throw new Exception( self::ERROR_SESSION_CLOSE_FILE );
  140. } else {
  141. return true;
  142. }
  143.  
  144. }
  145. catch ( Exception $e )
  146. {
  147. die( $this->session_error() );
  148. }
  149.  
  150. }
  151.  
  152.  
  153. /**
  154. * create the name for the session file
  155. *
  156. * @access private
  157. * @return string
  158. */
  159. private function session_file_name( )
  160. {
  161. return self::$session_path . str_replace('.' , '_' , $_SERVER['REMOTE_ADDR']) . '.session';
  162. }
  163.  
  164.  
  165. /**
  166. * create a session id
  167. *
  168. * @access private
  169. * @return integer
  170. */
  171. private function create_session_id( )
  172. {
  173.  
  174. for( $i = 0 ; $i < self::$_sidl ; $i++ ) {
  175. $id .= rand(0,9);
  176. }
  177.  
  178. return (strlen($id) != self::$_sidl) ? 0 : $id;
  179.  
  180. }
  181.  
  182.  
  183. /**
  184. * make the session data
  185. * $mode = 0 (serialize) or 1 (unserialize)
  186. *
  187. * @param $data
  188. * @param $mode
  189. * @access private
  190. * @return string
  191. */
  192. private function session_data( $data , $mode = 0 )
  193. {
  194.  
  195. switch( $mode ) {
  196. default:
  197. return serialize( $data);
  198. case 0:
  199. return serialize( $data );
  200. case 1:
  201. return unserialize( $data );
  202. }
  203.  
  204. }
  205.  
  206.  
  207. /**
  208. * returns an error of the Exception class
  209. *
  210. * @access private
  211. * @param object $obj
  212. * @return string
  213. */
  214. private function session_error( Exception $obj )
  215. {
  216. $str = 'Error <b>' . $obj->getCode() . '</b>: ';
  217. $str .= $obj->getMessage();
  218. $str .= ' In <b>' . $obj->getFile() . '</b> ';
  219. $str .= 'at line <b>' . $obj->getLine() . '</b>';
  220.  
  221. return $str;
  222. }
  223.  
  224.  
  225. /**
  226. * check if the session is running
  227. *
  228. * @access private
  229. * @return true
  230. */
  231. private function session_check_alive( )
  232. {
  233.  
  234. return ( file_exists( $this->session_file_name() ) === true ) ? true : false;
  235.  
  236. }
  237.  
  238.  
  239. /**
  240. * update the history data. only when call to new session start
  241. *
  242. * @param integer $time
  243. * @param string $page
  244. * @access private
  245. * @return boolean
  246. */
  247. private function session_update_data_history( $time , $page )
  248. {
  249.  
  250. if( $this->session_check_alive() === false ) {
  251. return false;
  252. }
  253.  
  254. //let's get the data of the file
  255. $data = $this->session_get_data( $this->session_file_name() );
  256.  
  257. //$data is an array now (3D) we need to update the session_data (key) his value
  258. $data['session_data']['session_history'][$time] = $page;
  259.  
  260. //update the session file
  261. return ( $this->session_update_data( $this->session_file_name() , $data , true ) === false ) ? false : true;
  262.  
  263. }
  264.  
  265.  
  266. /**
  267. * get the data of the session file
  268. *
  269. * @access private
  270. * @param string $file
  271. * @param boolean $unserialize
  272. * @return string
  273. */
  274. private function session_get_data( $file , $unserialize = true )
  275. {
  276.  
  277. try {
  278.  
  279. if( touch( $file , time() , time() ) === false ) {
  280. throw new Exception( self::ERROR_SESSION_TOUCH_FILE );
  281. }
  282.  
  283. if( ($fp = fopen( $file , 'r' )) === false ) {
  284. throw new Exception( self::ERROR_SESSION_OPEN_FILE );
  285. }
  286.  
  287. while( !feof($fp) ) {
  288. $data = fgets($fp , 4096);
  289. }
  290.  
  291. if( fclose( $fp ) === false ) {
  292. throw new Exception( self::ERROR_SESSION_CLOSE_FILE );
  293. }
  294.  
  295. }
  296. catch ( Exception $e )
  297. {
  298. die( $this->session_error($e) );
  299. }
  300.  
  301. return ( $unserialize === true ) ? $this->session_data($data , 1) : $data;
  302.  
  303. }
  304.  
  305.  
  306. /**
  307. * update the new session data by inserting it in the session file
  308. *
  309. * @access private
  310. * @param string file
  311. * @param string $data
  312. * @param boolean $serialize
  313. * @return boolean
  314. */
  315. private function session_update_data( $file , $data , $serialze = false )
  316. {
  317.  
  318. if( $serialze === true ) {
  319. $data = $this->session_data( $data , 0 );
  320. }
  321.  
  322. try {
  323.  
  324. if( touch( $file , time() , time() ) === false ) {
  325. throw new Exception( self::ERROR_SESSION_TOUCH_FILE );
  326. }
  327.  
  328. if( unlink($file) === false ) {
  329. throw new Exception( self::ERROR_SESSION_DELETE_FILE );
  330. }
  331.  
  332. if( touch( $file , time() , time() ) === false ) {
  333. throw new Exception( self::ERROR_SESSION_TOUCH_FILE );
  334. }
  335.  
  336. if( ($fp = fopen( $file , 'w' )) === false ) {
  337. throw new Exception( self::ERROR_SESSION_OPEN_FILE );
  338. }
  339.  
  340. if( fwrite( $fp , $data ) === false ) {
  341. throw new Exception( self::ERROR_SESSION_WRITE_FILE );
  342. }
  343.  
  344. if( fclose( $fp ) === false ) {
  345. throw new Exception( self::ERROR_SESSION_CLOSE_FILE );
  346. }
  347.  
  348. }
  349. catch( Exception $e )
  350. {
  351. die( $this->session_error($e) );
  352. }
  353.  
  354. return true;
  355.  
  356. }
  357.  
  358.  
  359. /**
  360. * check if the session var has the correct pattern. no starting with a numbre and special chars or spaces
  361. *
  362. * @access private
  363. * @param string $var
  364. * @return boolean
  365. */
  366. private function session_check_var_name( $var )
  367. {
  368.  
  369. $pattern = '#^[a-zA-Z][a-zA-Z0-9\_]+#si';
  370.  
  371. return ( preg_match($pattern , $var) ) ? true : false;
  372.  
  373. }
  374.  
  375.  
  376.  
  377. /**
  378. * register a session var like $_SESSION['varName'] = varValue; (check clone OOP @php.net)
  379. *
  380. * @access public
  381. * @param string $name
  382. * @param mixed $value;
  383. * @return boolean
  384. */
  385. public function register_var( $name , $value )
  386. {
  387.  
  388. if( $this->session_check_alive() === false ) {
  389. return false;
  390. }
  391.  
  392. //check the var
  393. if( $this->session_check_var_name($name) === false ) {
  394. self::$session_return_last[] = false;
  395. }
  396.  
  397. //register the var by getting and updating the session file.
  398. //almost an clone of the session_update_data_history function
  399. $data = $this->session_get_data( $this->session_file_name() );
  400.  
  401. $data['session_data']['session_vars'][$name] = $value;
  402.  
  403. return ( $this->session_update_data( $this->session_file_name() , $data , true ) === false ) ? false : true;
  404.  
  405. }
  406.  
  407.  
  408. /**
  409. * check if the $name session var exists
  410. *
  411. * @param string $name
  412. * @return boolean
  413. */
  414. public function exists( $name )
  415. {
  416.  
  417. if( $this->session_check_alive() === false ) {
  418. return false;
  419. }
  420.  
  421. if( is_array($name) === true ) {
  422.  
  423. //recursie
  424. foreach ($name as $value) {
  425. $this->delete_var($value);
  426. }
  427.  
  428. } else {
  429.  
  430. //check if the var exists
  431. $data = $this->session_get_data( $this->session_file_name() );
  432.  
  433. return ( array_key_exists( $name , $data['session_data']['session_vars'] ) === true ) ? true : false;
  434.  
  435. }
  436.  
  437. }
  438.  
  439.  
  440. /**
  441. * change the value of an session var that exists
  442. * $force is when the key($name) doesn't exists it creates the value
  443. * $replace: 1 = vervangt de waarde met $value
  444. * 2 = voegt de waarde toe aan de rechterkant
  445. * 3 = voor floats/integers bijtellen met opgeven $value
  446. * 4 = voor floats/integers aftrekken met opgeven $value
  447. * 5 = voor floats/integers vermenigvuldigen met opgeven $value
  448. * 6 = voor floats/integers delen met opgeven $value
  449. * 7 = voor floats/integers modules met opgeven $value
  450. *
  451. * @access public
  452. * @param string $name
  453. * @param mixed $value;
  454. * @param integer $replace
  455. * @param boolean $force
  456. * @return boolean
  457. */
  458. public function update_var( $name , $value , $replace = 1 , $force = false )
  459. {
  460.  
  461. if( $this->session_check_alive() === false ) {
  462. self::$session_return_last[] = false;
  463. }
  464.  
  465. //check the var
  466. if( $this->session_check_var_name($name) === false ) {
  467. return false;
  468. }
  469.  
  470. //register the var by getting and updating the session file.
  471. $data = $this->session_get_data( $this->session_file_name() );
  472.  
  473. if( $force === true && $this->exists( $name ) === false ) {
  474.  
  475. $data['session_data']['session_vars'][$name] = $value;
  476.  
  477. } elseif( $this->exists( $name ) === true ) {
  478.  
  479. switch($replace) {
  480.  
  481. default: $data['session_data']['session_vars'][$name] = $value;
  482. break;
  483.  
  484. case 1: $data['session_data']['session_vars'][$name] = $value;
  485. break;
  486.  
  487. case 2: $data['session_data']['session_vars'][$name] .= $value;
  488. break;
  489.  
  490. case 3: $data['session_data']['session_vars'][$name] += $value;
  491. break;
  492.  
  493. case 4: $data['session_data']['session_vars'][$name] -= $value;
  494. break;
  495.  
  496. case 5: $data['session_data']['session_vars'][$name] *= $value;
  497. break;
  498.  
  499. case 6: $data['session_data']['session_vars'][$name] /= $value;
  500. break;
  501.  
  502. case 7: $data['session_data']['session_vars'][$name] %= $value;
  503. break;
  504.  
  505. }
  506.  
  507. } else {
  508. return false;
  509. }
  510.  
  511. return ( $this->session_update_data( $this->session_file_name() , $data , true ) === false ) ? false : true;
  512.  
  513. }
  514.  
  515.  
  516. /**
  517. * delete a session var
  518. *
  519. * @access public
  520. * @param string $name
  521. * @return boolean
  522. */
  523. public function delete_var( $name )
  524. {
  525.  
  526. if( $this->session_check_alive() === false ) {
  527. self::$session_return_last[] = false;
  528. }
  529.  
  530. //check the var
  531. if( $this->session_check_var_name($name) === false ) {
  532. return false;
  533. }
  534.  
  535. //register the var by getting and updating the session file.
  536. $data = $this->session_get_data( $this->session_file_name() );
  537.  
  538. unset( $data['session_data']['session_vars'][$name] );
  539.  
  540. return ( $this->session_update_data( $this->session_file_name() , $data , true ) === false ) ? false : true;
  541.  
  542. }
  543.  
  544.  
  545. /**
  546. * get the value of a session var
  547. *
  548. * @access public
  549. * @param string $name
  550. * @return mixed
  551. */
  552. public function get_var( $name )
  553. {
  554.  
  555. if( $this->session_check_alive() === false ) {
  556. return false;
  557. }
  558.  
  559. //check the var
  560. if( $this->session_check_var_name($name) === false ) {
  561. return false;
  562. }
  563.  
  564. //register the var by getting and updating the session file.
  565. $data = $this->session_get_data( $this->session_file_name() );
  566.  
  567. return $data['session_data']['session_vars'][$name];
  568.  
  569.  
  570. }
  571.  
  572.  
  573. /**
  574. * get the value of a session var
  575. * the return is an array (if check_alive is true) -> [array] [key : time] _-_ [value : url]
  576. *
  577. * @access public
  578. * @return mixed
  579. */
  580. public function get_history( )
  581. {
  582.  
  583. if( $this->session_check_alive() === false ) {
  584. return false;
  585. }
  586.  
  587. //get the history
  588. $data = $this->session_get_data( $this->session_file_name() );
  589.  
  590. return $data['session_data']['session_history'];
  591.  
  592.  
  593. }
  594.  
  595.  
  596. /**
  597. * set the values of the session vars to empty - reset
  598. *
  599. * @access public
  600. * @param boolean $reset_history
  601. * @return boolean
  602. */
  603. public function reset( $reset_history = false )
  604. {
  605.  
  606. if( $this->session_check_alive() === false ) {
  607. return false;
  608. }
  609.  
  610. //reset all variables
  611. $data = $this->session_get_data( $this->session_file_name() );
  612.  
  613. foreach( $data['session_data']['session_vars'] as $key => $value ) {
  614. $data['session_data']['session_vars'][$key] = '';
  615. }
  616.  
  617. //history?
  618. if( $reset_history === true ) {
  619.  
  620. foreach( $data['session_data']['session_history'] as $key => $value ) {
  621. unset( $data['session_data']['session_history'] );
  622. }
  623.  
  624. }
  625.  
  626. return ( $this->session_update_data( $this->session_file_name() , $data , true ) === false ) ? false : true;
  627.  
  628. }
  629.  
  630.  
  631. /**
  632. * checks the session timeout. if it's timeout it destroy the session and creates new one
  633. *
  634. * @access private
  635. * @return mixed
  636. */
  637. private function session_check_timeout( )
  638. {
  639.  
  640. if( $this->session_check_alive() === false ) {
  641. return false;
  642. }
  643.  
  644. //get the latest key of the session_history
  645. $data = $this->session_get_data( $this->session_file_name() );
  646. $keys = array_keys( $data['session_data']['session_history'] );
  647.  
  648. $dif = time() - $keys[count($keys)-1];
  649.  
  650. if( $dif > $data['session_timeout'] ) {
  651. $this->destroy();
  652. }
  653.  
  654. }
  655.  
  656.  
  657. /**
  658. * checks the session timeout with great force. means he looks to every session file
  659. *
  660. * @access private
  661. * @return mixed
  662. */
  663. private function session_check_timeout_force( )
  664. {
  665.  
  666. if( $this->session_check_alive() === false ) {
  667. return false;
  668. }
  669.  
  670. //force that session
  671. $dir = opendir( self::$session_path );
  672.  
  673. while( ($file = readdir( $dir )) !== false) {
  674. if( $file != '.' && $file != '..' ) {
  675. $data = $this->session_get_data( self::$session_path . $file );
  676. $keys = array_keys( $data['session_data']['session_history'] );
  677.  
  678. $dif = time() - $keys[count($keys)-1];
  679.  
  680. if( $dif > $data['session_timeout'] && $data['session_timeout'] != 0 ) {
  681. $this->destroy();
  682. }
  683. }
  684. }
  685.  
  686. closedir($dir);
  687.  
  688. }
  689.  
  690.  
  691. /**
  692. * destroy the session
  693. *
  694. * @access public
  695. * @return boolean
  696. */
  697. public function destroy( )
  698. {
  699.  
  700. if( $this->session_check_alive() === false ) {
  701. return false;
  702. }
  703.  
  704. //destroy
  705. try {
  706.  
  707. if( touch( $this->session_file_name() , time() , time() ) === false ) {
  708. throw new Exception( self::ERROR_SESSION_TOUCH_FILE );
  709. }
  710.  
  711. if( unlink( $this->session_file_name() ) === false ) {
  712. throw new Exception( self::ERROR_SESSION_DELETE_FILE );
  713. }
  714.  
  715. }
  716. catch ( Exception $e )
  717. {
  718. die( $this->session_error($e) );
  719. }
  720.  
  721. return true;
  722.  
  723. }
  724.  
  725. }
  726. ?>


voorbeeld #01(login.php)
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4.  * an example (login) of the usage of the session class
  5.  *
  6.  *
  7.  * @license GNU/GPL http://www.gnu.org/licenses/gpl.html
  8.  * @author stijn1989 <stijnleenknegt@gmail.com>
  9.  * @version Versie 1.0
  10.  * @package Session
  11.  */
  12.  
  13. //start a new session
  14. include('session.class.php');
  15. $session = new Session( 300 , false );
  16.  
  17. //login gegevens
  18. $uname = 'stijn';
  19. $pwd = md5('stijn');
  20.  
  21. //logout function
  22. function logout( $destroy = false )
  23. {
  24.  
  25. global $session;
  26.  
  27. if( $session->delete_var('username') === true && $session->delete_var('logged_in') === true ) {
  28. echo "uitgelogd!";
  29. }
  30.  
  31. if( $destroy === true ) {
  32. if( $session->destroy() === true ) {
  33. echo " session destoried!";
  34. }
  35. header('location: login.php');
  36. }
  37.  
  38. }
  39.  
  40. //check if we are logged in
  41. if( $session->get_var('logged_in') === true && $session->get_var('username') == $uname ) {
  42. echo "<a href=\"?logout=1\">logout</a>";
  43.  
  44. if( $_GET['logout'] == 1 ) {
  45. logout(true);
  46.  
  47. }
  48.  
  49. } else {
  50.  
  51. //login
  52. if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
  53.  
  54. if( empty($_POST['username']) === true || isset($_POST['username']) === false ) {
  55. die('You must enter a username to login!');
  56. }
  57.  
  58. if( empty($_POST['password']) === true || isset($_POST['password']) === false ) {
  59. die('You must enter a password to login!');
  60. }
  61.  
  62. if( $_POST['username'] != $uname || md5($_POST['password']) != $pwd ) {
  63. die('password or username is wrong!');
  64. }
  65.  
  66. //passed the checkpoints
  67. $session->register_var('username' , $_POST['username']);
  68. $session->register_var('logged_in' , true);
  69.  
  70. //refresh
  71. header('location: login.php');
  72.  
  73. } else {
  74. ?>
  75. <form action="<?php echo $PHP_SELF; ?>" method="POST">
  76. <table width="100%" cellpadding="0" cellspacing="0">
  77.  
  78. <tr>
  79. <td width="50%" align="right">Username:</td>
  80. <td width="50%" align="left"><input type="text" name="username" size="30"></td>
  81. </tr>
  82.  
  83. <tr>
  84. <td width="50%" align="right">Password:</td>
  85. <td width="50%" align="left"><input type="password" name="password" size="30"></td>
  86. </tr>
  87.  
  88. <tr>
  89. <td colspan="2" align="center"><input type="submit" name="submit" value="Log-in"></td>
  90. </tr>
  91.  
  92. </table>
  93. </form>
  94. <?php
  95. }
  96. }
  97. ?>


voorbeeld #02(hits_count.php)
  1. <?php
  2.  
  3. include('session.class.php');
  4. include('array_dump.php');
  5.  
  6. if( isset( $_GET['destroy'] ) === true && $_GET['destroy'] == 1 ) {
  7. $session->destroy();
  8. }
  9.  
  10. $session = new Session(300);
  11.  
  12. if( $session->exists( 'count' ) === false || $session->exists( 'hits' ) === false ) {
  13. $session->register_var('count' , 0);
  14. $session->register_var('hits' , 0);
  15. }
  16.  
  17. if( $session->exists( 'count' ) === true && $session->exists( 'hits' ) === true ) {
  18.  
  19. if( $session->get_var('hits') < 10 ) {
  20.  
  21. $session->update_var('hits' , 1 , 3 , false);
  22.  
  23. //update the session var with big for
  24. for($i=0;$i<100;$i++) {
  25. $session->update_var('count' , $i , 3 , false );
  26. }
  27.  
  28. echo $session->get_var('count') . ' hits: ' . $session->get_var('hits');
  29.  
  30. } else { //destroy session afther 10 times
  31.  
  32. echo ( $session->reset( true ) === true ) ? 'sessie gereset' : 'sessie werd niet gereset. klik <a href=\"?destroy=1\">hier</a> om deze te vernietigen';
  33.  
  34. }
  35.  
  36. }
  37. ?>
  38. <br /><br />
  39. <h2 align="center">Session history</h2>
  40. <?php
  41.  
  42. //array_dump: http://www.sitemasters.be/?pagina=scripts/scripts&cat=23&id=1104
  43. array_dump( $session->get_history() );
  44.  
  45. ?>
Download code! Download code (.txt)

Download dit script!
 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (6)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.09s