search
HomeBackend DevelopmentPHP Tutorial 两个项目(分别在两台服务器下),如何给它们做同一个登陆功能呢?求思路

两个项目(分别在两台服务器上),怎么给它们做同一个登陆功能呢?求思路
项目a:部署在192.168.0.1上
项目b:部署在192.168.0.2上

现在要做个项目c,要实现的功能是登陆功能:a、b项目都要通过c登陆以后才能访问。

登陆状态我准备保存到Cookie中,但是a和b在两台不同的服务器上,要怎么传递这个会话(Cookie)呢?
求思路,谢谢!

------解决方案--------------------
如果一天后仍未有满意答案,我就来解答
------解决方案--------------------
session写在表中或在内存缓存中 只在一个地方维护用户的状态
------解决方案--------------------
如果是一二级域名 设置cookie的作用域为根域名
不同的域名可将sessionid跟在url后面 再进行验证
session写入表的时候 取头信息加IP进行加密 在直接输域名的情况下取这一段信息进行验证
类似这样 sprintf('%08x', crc32(!empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] . ROOT_PATH . $ip . $session_id : ROOT_PATH . $ip . $session_id));

版主说不定有更好的解决方法
------解决方案--------------------
你可以去研究一下单点登录 
我们的设想是将 PHP 的 Session 数据集中存储,这样对于不同服务器中运行的 PHP 来说,只有一个共有的 Session 数据库,那么用户在服务器 A 登录所生成的 Session 数据在服务器 B、C、D 等服务器都可以共享,就可以免除多次登录。但由于 PHP 的 Session 是需要 Cookie 的,而 Cookie 又是与域名相关的,所以采用这个方案的各个服务器需要有相同的域名(至少是相同的二级域名),比如:
1、所有服务器的域名都是 www.whybsd.com,这个东东 DNS 轮询就可以实现;这个时候,在 PHP 中将 Cookie 域名设置为 www.whybsd.com 即可;
2、所有服务器的域名都是以 .whybsd.com 结尾的三级域名,比如 a.whybsd.com,b.whybsd.com 等等,这个时候,在 PHP 中将 Cookie 域名设置为 .whybsd.com 就可以共享 Cookie 了。
------解决方案--------------------
用CAS
------解决方案--------------------
http://www.phpweblog.net/linxiaobo/archive/2009/04/15/6419.html
这是常见的一种,地址我随便搜的,P3P 后面的一串记不下来,你可以看看。稍微看了一下,只要P3P设置正确,肯定可以。这种方案个人解决过跨域cookie问题。

需要注意两个域名对应的地方。。不加P3P,IE,Safari【不确定】通不过的,这大概叫第三方cookie?


另外还可以是借助iframe【这可没有跨域的概念】,不同的域父子框架之间传值,可通过url锚点……思路自己扩展咯

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment