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

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

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.5 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    elseif ( $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( $pseudo )
52{
53  global $conf,$prefixeTable,$REMOTE_ADDR;
54  // 1. trouver une clé de session inexistante
55  $id_found = false;
56  while ( !$id_found )
57  {
58    $generated_id = generate_key();
59    $query = 'select id';
60    $query.= ' from '.$prefixeTable.'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. récupération de l'id de l'utilisateur dont le pseudo
69  //    est passé en paramètre
70  $query = 'select id';
71  $query.= ' from '.$prefixeTable.'users';
72  $query.= " where pseudo = '".$pseudo."';";
73  $row = mysql_fetch_array( mysql_query( $query ) );
74  $user_id = $row['id'];
75  // 3. insertion de la session dans la base de donnée
76  $expiration = $conf['session_time']*60+time();
77  $query = 'insert into '.$prefixeTable.'sessions';
78  $query.= ' (id,user_id,expiration,ip) values';
79  $query.= "('".$generated_id."','".$user_id;
80  $query.= "','".$expiration."','".$REMOTE_ADDR."');";
81  mysql_query( $query );
82               
83  return $generated_id;
84}
85       
86function add_session_id_to_url( $url, $redirect = false )
87{
88  global $page, $user;
89  $amp = "&amp;";
90  if ( $redirect )
91  {
92    $amp = "&";
93  }
94  if ( !$user['is_the_guest'] )
95  {
96    if ( ereg( "\.php\?",$url ) )
97    {
98      return $url.$amp."id=".$page['session_id'];
99    }
100    else
101    {
102      return $url."?id=".$page['session_id'];
103    }
104  }
105  else
106  {
107    return $url;
108  }
109}
110
111function add_session_id( $url, $redirect = false )
112{
113  global $page, $user;
114  $amp = "&amp;";
115  if ( $redirect )
116  {
117    $amp = "&";
118  }
119  if ( !$user['is_the_guest'] )
120  {
121    if ( ereg( "\.php\?",$url ) )
122    {
123      return $url.$amp."id=".$page['session_id'];
124    }
125    else
126    {
127      return $url."?id=".$page['session_id'];
128    }
129  }
130  else
131  {
132    return $url;
133  }
134}
135?>
Note: See TracBrowser for help on using the repository browser.