使用session_set_save_handler()函数,将Session的内容写入数据库
<span> 1</span> <?<span>php </span><span> 2</span> <span>/*</span> <span> 3</span> <span> *@author Fahy </span><span> 4</span> <span> *@link http://home.cnblogs.com/u/HuangWj </span><span> 5</span> <span> *数据库为mysql, </span><span> 6</span> <span> *数据库名为session,表名为session, </span><span> 7</span> <span> *表中字段包括PHPSESSID,update_time,client_ip,data </span><span> 8</span> <span>*/</span> <span> 9</span> <span>class</span><span> Session{ </span><span> 10</span> <span>private</span> <span>static</span> <span>$handler</span> = <span>null</span><span>; </span><span> 11</span> <span>private</span> <span>static</span> <span>$ip</span> = <span>null</span><span>; </span><span> 12</span> <span>private</span> <span>static</span> <span>$lifetime</span> = <span>null</span><span>; </span><span> 13</span> <span>private</span> <span>static</span> <span>$time</span> = <span>null</span><span>; </span><span> 14</span> <span> 15</span> <span>//</span><span>配置静态变量</span> <span> 16</span> <span>private</span> <span>static</span> <span>function</span> init(<span>$handler</span><span>){ </span><span> 17</span> self::<span>$handler</span> = <span>$handler</span>; <span>//</span><span>获取数据库资源</span> <span> 18</span> self::<span>$ip</span> = !<span>empty</span>(<span>$_SERVER</span>["REMOTE_ADDR"])? <span>$_SERVER</span>["REMOTE_ADDR"]:'unkonw'; <span>//</span><span>获取客户端ip</span> <span> 19</span> self::<span>$lifetime</span> = <span>ini_get</span>('session.gc_maxlifetime'); <span>//</span><span>获取session生命周期</span> <span> 20</span> self::<span>$time</span> = <span>time</span>(); <span>//</span><span>获取当前时间</span> <span> 21</span> <span> } </span><span> 22</span> <span>//</span><span>调用session_set_save_handler()函数并开启session</span> <span> 23</span> <span>static</span> <span>function</span> start(<span>$pdo</span><span>){ </span><span> 24</span> self::init(<span>$pdo</span><span>); </span><span> 25</span> <span>session_set_save_handler</span><span>( </span><span> 26</span> <span>array</span>(<span>__CLASS__</span>,'open'), <span> 27</span> <span>array</span>(<span>__CLASS__</span>,'close'), <span> 28</span> <span>array</span>(<span>__CLASS__</span>,'read'), <span> 29</span> <span>array</span>(<span>__CLASS__</span>,'write'), <span> 30</span> <span>array</span>(<span>__CLASS__</span>,'destroy'), <span> 31</span> <span>array</span>(<span>__CLASS__</span>,'gc'<span>) </span><span> 32</span> <span> ); </span><span> 33</span> <span>session_start</span><span>(); </span><span> 34</span> <span> } </span><span> 35</span> <span> 36</span> <span>public</span> <span>static</span> <span>function</span> open(<span>$path</span>,<span>$name</span><span>){ </span><span> 37</span> <span>return</span> <span>true</span><span>; </span><span> 38</span> <span> } </span><span> 39</span> <span>public</span> <span>static</span> <span>function</span><span> close(){ </span><span> 40</span> <span>return</span> <span>true</span><span>; </span><span> 41</span> <span> } </span><span> 42</span> <span> 43</span> <span>//</span><span>查询数据库中的数据</span> <span> 44</span> <span>public</span> <span>static</span> <span>function</span> read(<span>$PHPSESSID</span><span>){ </span><span> 45</span> <span>$sql</span> = "select PHPSESSID,update_time,client_ip,data from session where PHPSESSID=?"<span>; </span><span> 46</span> <span>$stmt</span> = self::<span>$handler</span>->prepare(<span>$sql</span><span>); </span><span> 47</span> <span>$stmt</span>->execute(<span>array</span>(<span>$PHPSESSID</span><span>)); </span><span> 48</span> <span>if</span>(!<span>$result</span> = <span>$stmt</span>->fetch(PDO::<span>FETCH_ASSOC)){ </span><span> 49</span> <span>return</span> ''<span>; </span><span> 50</span> <span> } </span><span> 51</span> <span>if</span>(self::<span>$ip</span> == <span>$result</span>['client_ip'<span>]){ </span><span> 52</span> self::destroy(<span>$PHPSESSID</span><span>); </span><span> 53</span> <span>return</span> ''<span>; </span><span> 54</span> <span> } </span><span> 55</span> <span>if</span>((<span>$result</span>['update_time']+self::<span>$lifetime</span>)<self::<span>$time</span><span>){ </span><span> 56</span> self::destroy(<span>$PHPSESSID</span><span>); </span><span> 57</span> <span>return</span> ''<span>; </span><span> 58</span> <span> } </span><span> 59</span> <span>return</span> <span>$result</span>['data'<span>]; </span><span> 60</span> <span> } </span><span> 61</span> <span>/*</span> <span> 62</span> <span> *首先查询该session是否存在数据,如果存在,则更新数据,如果不存在,则插入数据 </span><span> 63</span> <span>*/</span> <span> 64</span> <span>//</span><span>将session写入数据库中,$data传入session中的keys和values数组</span> <span> 65</span> <span>public</span> <span>static</span> <span>function</span> write(<span>$PHPSESSID</span>,<span>$data</span><span>){ </span><span> 66</span> <span>$sql</span> = "select PHPSESSID,update_time,client_ip,data from session where PHPSESSID=?"<span>; </span><span> 67</span> <span>$stmt</span> = self::<span>$handler</span>->prepare(<span>$sql</span><span>); </span><span> 68</span> <span>$stmt</span>->execute(<span>array</span>(<span>$PHPSESSID</span><span>)); </span><span> 69</span> <span> 70</span> <span>if</span>(<span>$result</span>=<span>$stmt</span>->fetch(PDO::<span>FETCH_ASSOC)){ </span><span> 71</span> <span>if</span>(<span>$result</span>['data'] != <span>$data</span> || self::<span>$time</span> > (<span>$result</span>['update_time']+30<span>)){ </span><span> 72</span> <span>$sql</span> = "update session set update_time=?,data=? where PHPSESSID = ?"<span>; </span><span> 73</span> <span>$stmt</span> = self::<span>$handler</span>->prepare(<span>$sql</span><span>); </span><span> 74</span> <span>$stmt</span>->execute(<span>array</span>(<span>$self</span>::<span>$time</span>,<span>$data</span>,<span>$PHPSESSID</span><span>)); </span><span> 75</span> <span> } </span><span> 76</span> }<span>else</span><span>{ </span><span> 77</span> <span>if</span>(!<span>empty</span>(<span>$data</span><span>)){ </span><span> 78</span> <span>try</span><span>{ </span><span> 79</span> <span>$sql</span> = "insert into session(PHPSESSID,update_time,client_ip,data) values(?,?,?,?)"<span>; </span><span> 80</span> }<span>catch</span>(PDOException <span>$e</span><span>){ </span><span> 81</span> <span>echo</span> <span>$e</span>-><span>getMessage(); </span><span> 82</span> <span> } </span><span> 83</span> <span>$sth</span> = self::<span>$handler</span>->prepare(<span>$sql</span><span>); </span><span> 84</span> <span>$sth</span>->execute(<span>array</span>(<span>$PHPSESSID</span>,self::<span>$time</span>,self::<span>$ip</span>,<span>$data</span><span>)); </span><span> 85</span> <span> } </span><span> 86</span> <span> } </span><span> 87</span> <span>return</span> <span>true</span><span>; </span><span> 88</span> <span> } </span><span> 89</span> <span> 90</span> <span>public</span> <span>static</span> <span>function</span> destroy(<span>$PHPSESSID</span><span>){ </span><span> 91</span> <span>$sql</span> = "delete from session where PHPSESSID = ?"<span>; </span><span> 92</span> <span>$stmt</span> = self::<span>$handler</span>->prepare(<span>$sql</span><span>); </span><span> 93</span> <span>$stmt</span>->execute(<span>array</span>(<span>$PHPSESSID</span><span>)); </span><span> 94</span> <span>return</span> <span>true</span><span>; </span><span> 95</span> <span> } </span><span> 96</span> <span>public</span> <span>static</span> <span>function</span> gc(<span>$lifetime</span><span>){ </span><span> 97</span> <span>$sql</span> = "delete from session where update_time<?"<span>; </span><span> 98</span> <span>$stmt</span> = self::<span>$handler</span>->prepare(<span>$sql</span><span>); </span><span> 99</span> <span>$stmt</span>->execute(<span>array</span>(self::<span>$time</span>-<span>$lifetime</span><span>)); </span><span>100</span> <span>return</span> <span>true</span><span>; </span><span>101</span> <span> } </span><span>102</span> <span> } </span><span>103</span> <span>//</span><span>使用PDO连接数据库</span> <span>104</span> <span>try</span><span>{ </span><span>105</span> <span>$pdo</span> = <span>new</span> PDO("mysql:host=localhost;dbname=session","root","hwj193"<span>); </span><span>106</span> }<span>catch</span>(PDOException <span>$e</span><span>){ </span><span>107</span> <span>echo</span> <span>$e</span>-><span>getMessage(); </span><span>108</span> <span> } </span><span>109</span> <span>//</span><span>传递数据库资源</span> <span>110</span> Session::start(<span>$pdo</span>);