search
HomeBackend DevelopmentPHP Tutorialphp写webservice,验证问题如何解决?

查了一些资料,好像都是每一次调用webservice方法时,都需要附带客户验证信息,能否像普通的网页浏览那样,登录后,在会话期内任意访问都不需要再次提供客户验证信息呢?


回复讨论(解决方案)

在session中保留id

那是提供webservice方决定的!

那是提供webservice方决定的!

不单是提供webservice方决定,还有访问方式,比如使用basic验证,用浏览器访问的话,会提示一个验证框,如果验证通过,同一进程再次访问就不会提示框了。

我的问题在于curl请求webservice的话,似乎就必须每次都提供验证信息来完成调用。有没有一次验证的解决方案呢?

你查查看cookie,HTTP header中有没有id之类的值,每次请求的时候都要将这个值发给服务器

你查查看cookie,HTTP header中有没有id之类的值,每次请求的时候都要将这个值发给服务器

思路还是不够完整

你说的是浏览器验证 
用户:$_SERVER['PHP_AUTH_USER']  
密码:$_SERVER['PHP_AUTH_PW']

参考:http://php.net/manual/en/features.http-auth.php

function authenticate() {    header('WWW-Authenticate: Basic realm=""');    header('HTTP/1.0 401 Unauthorized');    echo "You must enter a valid login ID and password to access this resource\n";    exit;}if (addslashes($_SERVER['PHP_AUTH_USER'])!= 'adminname' || addslashes($_SERVER['PHP_AUTH_PW'])!= 'adminadmin') {	header('WWW-Authenticate: Basic realm=""');	header('HTTP/1.0 401 Unauthorized');	echo "对不起,无权进入! user=".$_SERVER['PHP_AUTH_USER']."password=".$_SERVER['PHP_AUTH_PW'];  //后面这半句我是为了测试而加上去的;}else{	echo "进入! user=".$_SERVER['PHP_AUTH_USER']."password=".$_SERVER['PHP_AUTH_PW'];  //后面这半句我是为了测试而加上去的;	echo "<p>Welcome: {$_SERVER['PHP_AUTH_USER']}<br />";//   echo "Old: {$_REQUEST['OldAuth']}";//   echo "<form action='' METHOD='post'>\n";//   echo "<input type='hidden' name='SeenBefore' value='1' />\n";//   echo "<input type='hidden' name='OldAuth' value='{$_SERVER['PHP_AUTH_USER']}' />\n";//   echo "<input type='submit' value='Re authenticate' />\n";//   echo "</form></p>\n";	echo date('h:i:s') . "<br />";	//暂停 10 秒	sleep(10);		//重新开始	echo date('h:i:s');	header('WWW-Authenticate: Basic realm=""');	header('HTTP/1.0 401 Unauthorized');        }//可以直接使用试试看是不是你想要的结果

你说的是浏览器验证 
用户:$_SERVER['PHP_AUTH_USER']  
密码:$_SERVER['PHP_AUTH_PW']

参考:http://php.net/manual/en/features.http-auth.php


应该不是我要的。我想问的包含两方面的问题:

1、开放的webservice有哪些验证机制?比如我说key是一种,http的basic验证也是一种,还有其他哪些方式呢?

2、想知道有哪些验证机制是为了这个目的:有没有一种验证机制,在php调用webservice时,只需在第一次请求中发送验证信息,以后调用就不需要再次验证了--类似于普通页面浏览的过程,填写一次登陆信息后,只要不超时,访问这个网站的其他任何页面都不需要再登陆。

引用 7 楼 ihefe 的回复:

你说的是浏览器验证
用户:$_SERVER['PHP_AUTH_USER']
密码:$_SERVER['PHP_AUTH_PW']

参考:http://php.net/manual/en/features.http-auth.php



应该不是我要的。我想问的包含两方面的问题:

1、开放的webservice有哪些验证机制?比……
这个就是只验证一次, 你试过我给你的代码吗?把最后
    echo date('h:i:s');
    header('WWW-Authenticate: Basic realm=""');
    header('HTTP/1.0 401 Unauthorized');

去掉

呃也就是你说的http basic验证

今天回头来看这个问题,似乎可以用curl发送cookie的方式解决。

myservice.php

myproxy.php

myclient.php

用户在myclient.php(或者其他地方登陆),通过后可直接访问myproxy.php,在myproxy.php中通过curl的方式连同cookie一起请求myservice.php。

各位的意见如何?

lz说的完全可以采用session机制来实现,当你发送请求时,在服务器端建立session,服务器处理完用户请求后,将sid发送给客户端,客户端可以记录在cookie里,只要sid不过期,就不用重复发送验证信息了。session就是提供这种机制的。

lz说的完全可以采用session机制来实现,当你发送请求时,在服务器端建立session,服务器处理完用户请求后,将sid发送给客户端,客户端可以记录在cookie里,只要sid不过期,就不用重复发送验证信息了。session就是提供这种机制的。


但是session机制怎样应用到webservice中呢?

在session中保留id

在session中保留id

还是不够清楚,和我说的通过“curl代理将验证传递的方式”有什么不同吗?

不用每次都传密码的,这样有安全隐患,可以用临时的token来解决

直接不启用cookies啦、  session啦 等类似的机制不行吗 ?

哎,PHP就是没有完善的东西呀。

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
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality.Customizing/Extending Frameworks: How to add custom functionality.Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function