source: trunk/include/functions_session.inc.php @ 14

Last change on this file since 14 was 14, checked in by z0rglub, 21 years ago

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.3 KB
Line 
1<?php
2/***************************************************************************
3 *                         functions_session.inc.php                       *
4 *                            -------------------                          *
5 *   application          : PhpWebGallery 1.3                              *
6 *   author               : Pierrick LE GALL <pierrick@z0rglub.com>        *
7 *                                                                         *
8 ***************************************************************************
9
10 ***************************************************************************
11 *                                                                         *
12 *   This program is free software; you can redistribute it and/or modify  *
13 *   it under the terms of the GNU General Public License as published by  *
14 *   the Free Software Foundation;                                         *
15 *                                                                         *
16 ***************************************************************************/
17function generate_key()
18{
19  global $conf;
20  $md5 = md5( substr( microtime(), 2, 6 ).$conf['session_keyword'] );
21  $init = '';
22  for ( $i = 0; $i < strlen( $md5 ); $i++ )
23  {
24    if ( is_numeric( $md5[$i] ) )
25    {
26      $init.= $md5[$i];
27    }
28  }
29  $init = substr( $init, 0, 8 );
30  mt_srand( $init );
31  $key = '';
32  for ( $i = 0; $i < $conf['session_id_size']; $i++ )
33  {
34    $c = mt_rand( 0, 2 );
35    if ( $c == 0 )
36    {
37      $key .= chr( mt_rand( 65, 90 ) );
38    }
39    else if ( $c == 1 )
40    {
41      $key .= chr( mt_rand( 97, 122 ) );
42    }
43    else
44    {
45      $key .= mt_rand( 0, 9 );
46    }
47  }
48  return $key;
49}
50       
51function session_create( $username )
52{
53  global $conf;
54  // 1. searching an unused sesison key
55  $id_found = false;
56  while ( !$id_found )
57  {
58    $generated_id = generate_key();
59    $query = 'select id';
60    $query.= ' from '.PREFIX_TABLE.'sessions';
61    $query.= " where id = '".$generated_id."';";
62    $result = mysql_query( $query );
63    if ( mysql_num_rows( $result ) == 0 )
64    {
65      $id_found = true;
66    }
67  }
68  // 2. retrieving id of the username given in parameter
69  $query = 'select id';
70  $query.= ' from '.PREFIX_TABLE.'users';
71  $query.= " where username = '".$username."';";
72  $row = mysql_fetch_array( mysql_query( $query ) );
73  $user_id = $row['id'];
74  // 3. inserting session in database
75  $expiration = $conf['session_time'] * 60 + time();
76  $query = 'insert into '.PREFIX_TABLE.'sessions';
77  $query.= ' (id,user_id,expiration,ip) values';
78  $query.= "('".$generated_id."','".$user_id;
79  $query.= "','".$expiration."','".$_SERVER['REMOTE_ADDR']."');";
80  mysql_query( $query );
81               
82  return $generated_id;
83}
84
85// add_session_id adds the id of the session to the string given in
86// parameter as $url. If the session id is the first parameter to the url,
87// it is preceded by a '?', else it is preceded by a '&amp;'. If the
88// parameter $redirect is set to true, '&' is used instead of '&'.
89function add_session_id( $url, $redirect = false )
90{
91  global $page, $user;
92  $amp = '&amp;';
93  if ( $redirect )
94  {
95    $amp = '&';
96  }
97  if ( !$user['is_the_guest'] )
98  {
99    if ( preg_match( '/\.php\?/',$url ) )
100    {
101      return $url.$amp.'id='.$page['session_id'];
102    }
103    else
104    {
105      return $url.'?id='.$page['session_id'];
106    }
107  }
108  else
109  {
110    return $url;
111  }
112}
113?>
Note: See TracBrowser for help on using the repository browser.