用php实现条形码登陆验证码
2011-03-11 15:49
477 查看
在前面的教程(ActiveResource探究(一))中,在服务器端每个用户都很直接,但是处理多用户的时候就会遇到麻烦,假如我有两个servers,一个处理REAST service,另一个处理html文件。但是ActiveResource假设认证实在类的层面上处理。这个时候ruby的元编程思想可以解决问题!
使用前面的例子,我扩展客户端如下:
这次允许我们为特定的用户创建特定的api,这样多个用户都可以链接到rest service了,来看看吧:
使用前面的例子,我扩展客户端如下:
require 'activeresource' module Sample module Client class API # # Creates a module that serves as an ActiveResource # client for the specified user # def self.create_api(login = nil, password = nil) # specify the site. Default to no credentials @url_base = "http://localhost:3000" @url_base = "http://#{login}:#{password}@localhost:3000" if login # build a module name. This assumes that logins are unique. # it also assumes they're valid ruby module names when capitalized @module = login ? login.capitalize : "Default" class_eval <<-"end_eval",__FILE__, __LINE__ module #{@module} class Post < ActiveResource::Base self.site = "#{@url_base}" end class Comment < ActiveResource::Base self.site = "#{@url_base}/posts/:post_id" end # return the module, not the last site String self end end_eval end end end end
这次允许我们为特定的用户创建特定的api,这样多个用户都可以链接到rest service了,来看看吧:
>> require 'ares_sample_client' => ["Sample"] >> api = Sample::Client::API.create_api => Sample::Client::API::Default >> p = api::Post.find(1) => #<Sample::Client::API::Default::Post:0xb715af74 @attributes={"updated_at"=>Wed Jan 09 02:36:34 UTC 2008, "id"=>1, "content"=>"The first post", "user_id"=>1, "created_at"=>Wed Jan 09 02:36:34 UTC 2008}, @prefix_options={}> >> p = api::Post.create(:content => "should fail") ActiveResource::UnauthorizedAccess: Failed with 401 Unauthorized from /usr/lib/ruby/gems/1.8/gems/activeresource-2.0.2/lib/active_resource/connection.rb:125:in `handle_response' from /usr/lib/ruby/gems/1.8/gems/activeresource-2.0.2/lib/active_resource/connection.rb:112:in `request' from /usr/lib/ruby/gems/1.8/gems/activeresource-2.0.2/lib/active_resource/connection.rb:101:in `post' from /usr/lib/ruby/gems/1.8/gems/activeresource-2.0.2/lib/active_resource/base.rb:803:in `create' from /usr/lib/ruby/gems/1.8/gems/activeresource-2.0.2/lib/active_resource/base.rb:636:in `save_without_validation' from /usr/lib/ruby/gems/1.8/gems/activeresource-2.0.2/lib/active_resource/validations.rb:262:in `save' from /usr/lib/ruby/gems/1.8/gems/activeresource-2.0.2/lib/active_resource/base.rb:339:in `create' from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/core_ext/object/misc.rb:28:in `returning' from /usr/lib/ruby/gems/1.8/gems/activeresource-2.0.2/lib/active_resource/base.rb:339:in `create' from (irb):4 >> auth_api = Sample::Client::API.create_api('test1','test1') => Sample::Client::API::Test1 >> p = auth_api::Post.find(1) => #<Sample::Client::API::Default::Post:0xb713dde8 @attributes={"updated_at"=>Wed Jan 09 02:36:34 UTC 2008, "id"=>1, "content"=>"The first post", "user_id"=>1, "created_at"=>Wed Jan 09 02:36:34 UTC 2008}, @prefix_options={}> >> p = auth_api::Post.create(:content => "should succeed!") => #<Sample::Client::API::Test1::Post:0xb713312c @attributes={"updated_at"=>Thu Jan 10 04:01:53 UTC 2008, "id"=>7, "content"=>"should succeed!", "user_id"=>nil, "created_at"=>Thu Jan 10 04:01:53 UTC 2008}, @prefix_options={}> >>
相关文章推荐
- php中实现登陆验证码
- PHP实现登陆表单提交CSRF及验证码
- php实现模拟登陆带验证码并且重定向的教务处系统
- 用PHP实现登陆验证码(类似条行码状)
- PHP实现登陆表单提交CSRF及验证码
- 用PHP实现登陆验证码(类似条行码状)
- PHP实现登陆验证码
- 用PHP实现登陆验证码(类似条行码状)
- php实现Ajax带有验证码的登陆注册功能
- 使用PHP实现登陆验证码
- Unity3D教程:Unity3D利用PHP+Mysql实现注册激活验证登陆
- PHP版单点登陆实现方案
- php实现验证码的识别(中级篇)
- php实现同一时间内一个账户只允许在一个终端登陆
- php 验证码实现
- php CI框架实现验证码功能和增强验证码安全性实战教程
- 登陆界面验证码实现
- php实现验证码的识别
- jsp实现登陆验证码
- PHP实现生成带背景的图形验证码功能