zoukankan      html  css  js  c++  java
  • php下 memcache实现跨域共享session

    一、两台服务器共享session

    在php.ini 配置文件中

    修改

    [Session] 

    session.save_handler= memcache   //之前是files

    session.save_path="tcp://192.168.20.1:11211"   //后面的是端口号,11211

    二、跨域

    在登陆的跳转按钮上加入

    <?php
    
    $sname = session_name();
    $sid= session_id();
    
    echo "<a href='http://www.linux.com/jiuxian/index.php?{$sname}={$sid}'>linux</a>";
    
    echo "<a href='http://www.win.com/jiuxian/index.php'>win</a>"
    ?>

    在登陆代码之前,加入如下代码

    <?php
    if($_GET[session_name()]){
    
    $mem = new Memcache;
    $mem ->connect('192.168.20.1','11211');
    $sessid=$_GET[session_name()];
    $_SESSION=$mem->get($sessid.'_data'); // 在session后加入data标志,便于识别。
    setcookie(session_name(),$sessid,0,'/')  //设置cookie,在根域名下都起作用,时间无限制。
    
    }else{
    
    session_start();
    }
    
    
    ?>

    登陆时,检查checkin的代码

    <?php
    //检索数据库找到相应记录
    //......
    
    
    // 找到后,进行的session处理,并且写入一份到memcache
    if($row){
        $_session['username'] = $row['username'];
        $_session['user_id'] = $row['id'];
    
        $mem =  new Memcache;
        $mem->connect("192.168.20.1","11211");
    
        $mem->set(session_id().'_data',$_SESSION);    //加入_data后缀名字,只是为了便于记忆识别
    
        echo "<script>location="index.php"</script>"
    
    }else{
        echo "<script>location="login.php"</script>"
    }
    
    
    ?>
  • 相关阅读:
    STM32系列命名规则
    在使用MOS管时要注意的问题
    LED汽车前大灯
    Linux Makefile analysis for plain usr
    Linux Kernel Makefile Test
    linux源码Makefile的详细分析
    "The connection for the USB device '###' was unsuccessful. The device is currently in use"
    Allegro使用技巧
    Integrated Circuit Intro
    ADC/DAC的一些参数
  • 原文地址:https://www.cnblogs.com/sdgtxuyong/p/12390815.html
Copyright © 2011-2022 走看看