如何存储用户的会话数据 [英] How to Store session data of user

查看:113
本文介绍了如何存储用户的会话数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网站的第一个问题(我是这个网站的新手)
被认为是发布我最困难的问题.....



我登录系统在我的网站成功登录后,我的受保护页面仅在登录后才显示,我想添加 $ _SESSION ['point'] 来存储user.and点并将其保存到数据库,如果用户点击链接,可以增加。我想将这个增加的点存储到我的userdb.php中。 (我没有使用MySql注册表单我使用了userdb.php文件)我的受保护页面的php代码是

 < code $<?php 
if(session_id()==)
{
session_start();
}
if(!isset($ _ SESSION ['username']))
{
header('Location:#');
出口;

if(isset($ _ SESSION ['expires_by']))
{
$ expires_by = intval($ _ SESSION ['expires_by']);
if(time()<$ expires_by)
{
$ _SESSION ['expires_by'] = time()+ intval($ _ SESSION ['expires_timeout']);
}
else
{
unset($ _ SESSION ['username']);
unset($ _ SESSION ['expires_by']);
unset($ _ SESSION ['expires_timeout']);
header('Location:#');
出口;


if(session_id()==)
{
session_start();

if(session_id()==)
{
session_start();
}
?>

我的display.php显示网址

 <?php 

mysql_connect('Server','user','passs');
mysql_select_db('add');
$ query = mysql_query('select * from addimage');

while($ row = mysql_fetch_assoc($ query))
{

echo'
< div style =min-width:300px; height:100px; border:red 5px; float:left;>'。$ row ['url']。 < / DIV>;
}

?>


解决方案

>

 <?php 
// if PHP> 5.4:if(PHP_SESSION_NONE == session_status()){
if('== == session_id()){
session_start();
}
if(isset($ _ SESSION ['expires_by'])){
$ expires_by = intval($ _ SESSION ['expires_by']);
if(time()<$ expires_by){
$ _SESSION ['expires_by'] = time()+ intval($ _ SESSION ['expires_timeout']);
} else {
session_destroy(); $!

$ b if(!isset($ _ SESSION ['username'])){
Header('Location:'。$ _SERVER ['REQUEST_URI']);
exit();
}
?>

然后点击URL,你可以使用jQuery和AJAX。你应该在你的CSS中声明一个类似link-block的类,然后写这样的URL


$ b pre $ echo $< div类= 链接块 > $行。[ 'URL'] '< / DIV>';

并且在包含jQuery脚本之后,在页面的onReady Javascript中为这些DIV添加点击处理函数: $ .post()。

  $('。link-block')。 ('/increase-points.php',{},function(retval){
if(retval.newpoints){
$('#point-block')。html(retval.newpoints);
}
});
});

增加点处理程序需要打开会话,它与上面的代码相同所以你可以把它放到一个外部包含session.php)中,然后打开数据库连接(另一个include ...),然后:

  UPDATE usertable SET points = points + 1 WHERE user_id = {$ _SESSION ['user_id']}; 

或者如果您只有用户名(确保它已正确转义)

  ... WHERE用户名='{$ escapedSessionUsername}'; 

顺便说一句,我需要添加 standard mysql _ * 弃用免责声明 b
$ b

之后,您可能会返回当前点数以显示为ID为points-block的DIV:

 您有< span id =points-block>< / span>点。 

通过从数据库中查询它们之后以JSON格式返回它(或者您可以将它们保留在会话中并更新数据库和会话;它为您节省了一个查询)

  // This in /update-points.php 
$ retval = array('newpoints'=> $ updated_points);
Header('Content-Type:application / json; charset = utf8');
die(json_encode($ retval));

您也可以通过其他方式做到这一点,但我在链接div中看不到锚点,所以我猜你想要的东西动态,这主要是指AJAX。


first question for the site(i am new to this site) thought to post my most difficult problem .....

I have Login system in my site after successful login my protected page is displayed only after login i want to add $_SESSION['point'] to store the point of user.and save it to data base and the point will be increased if user click link. I want to store this increased point into my userdb.php. where all sign up information i kept.(i have not used MySql for signup Form I have used userdb.php file)my protected page php code are

<?php
if (session_id() == "")
{
   session_start();
}
if (!isset($_SESSION['username']))
{
   header('Location: #');
   exit;
}
if (isset($_SESSION['expires_by']))
{
   $expires_by = intval($_SESSION['expires_by']);
   if (time() < $expires_by)
   {
      $_SESSION['expires_by'] = time() + intval($_SESSION['expires_timeout']);
   }
   else
   {
      unset($_SESSION['username']);
      unset($_SESSION['expires_by']);
      unset($_SESSION['expires_timeout']);
      header('Location: #');
      exit;
   }
}
if (session_id() == "")
{
   session_start();
}
if (session_id() == "")
{
   session_start();
}
?>

My display.php to show urls

<?php

mysql_connect('Server', 'user', 'passs');
mysql_select_db('add');
$query =mysql_query('select * from addimage');

while( $row = mysql_fetch_assoc($query) )
{

echo ' 
<div style="min-width:300px;height:100px;border:red 5px;float:left;">'.$row['url']. '</div>';
}

?>

解决方案

You can write your login PHP like,

<?php
    // if PHP > 5.4: if (PHP_SESSION_NONE == session_status()) {
    if ('' == session_id()) {
        session_start();
    }
    if (isset($_SESSION['expires_by'])) {
        $expires_by = intval($_SESSION['expires_by']);
        if (time() < $expires_by) {
            $_SESSION['expires_by'] = time() + intval($_SESSION['expires_timeout']);
        } else {
            session_destroy();
        }
    }
    if (!isset($_SESSION['username'])) {
        Header('Location: ' . $_SERVER['REQUEST_URI']);
        exit();
    }
?>

Then to click on the URLs you could perhaps use jQuery and AJAX. You should declare a class like "link-block" in your CSS, and write the URLs like this

echo '<div class="link-block">'.$row['url'].'</div>';

and add a click handler to those DIVs in the page's onReady Javascript, after including jQuery scripts:

$('.link-block').on('click', function(e) {
    $.post('/increase-points.php', { }, function(retval){
        if (retval.newpoints) {
            $('#point-block').html(retval.newpoints);
        }
    });
});

The increase-point handler needs to open the session, which is the same code as you have above (so you can put it into an external include "session.php"), and open the database connection (another include...), then:

UPDATE usertable SET points = points + 1 WHERE user_id = {$_SESSION['user_id']};

or if you have a username only (ensure it's properly escaped)

...WHERE username = '{$escapedSessionUsername}';

By the way, I need to add the standard mysql_* deprecation disclaimer.

After which, you might return the current points to be displayed into a DIV with id of "points-block":

    You have <span id="points-block"></span> points.

by returning it in JSON after querying them from the database (or you can keep them in session and update both DB and session; it saves you one query)

    // This in /update-points.php
    $retval = array('newpoints' => $updated_points);
    Header('Content-Type: application/json;charset=utf8');
    die(json_encode($retval));

You can do this in other ways too, but I saw no anchor in your link div, so I guess you want something dynamic, which mostly means AJAX.

这篇关于如何存储用户的会话数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆