url accessible only from mobile
i have web and mobile application. On my web application i have url address www.myweb.com/mobile/update which gen开发者_如何学Pythonerate xml for mobile application (java application, xml is used for updating data in this app). Is some way how to made these url accessible only from mobile (java application) and restrict it for others web users? I am using php and apache server. Thanks for you advices and sorry form my english. ;)
On you're specific page, you'll have to detect if the browser which is loading the page is a mobile one or not -- and, if it is, redirect the user to another page.
In order to detect if a user is using a mobile browser, you can use solutions such as WURFL.
But note that it's the browser itself that's indicating if it's a mobile one... so that information is not to be trusted.
Several mobile browsers allow the user to change the User-Agent that's sent to the server -- quite often, that feature used because the user feels his choice is better than the webmaster's one (I'm the best placed to judge if I want to see the mobile website, or the non-mobile one !)
Generally speaking, you should :
- Detect if a user is using a mobile device
- If he not is (and is on the mobile website), suggest him to visit the normal website
- If he is (and is on the normal website), suggest him to visit the mobile website.
But :
- Never trust the User-Agent.
- And let each user the choice.
The simplest possible solution would be checking the User-Agent header sent by the browser. However, I would not recommend outright restricting any access from non-mobile user agents, as there is a chance that someone will visit the site from a mobile device you don't know about. A more graceful solution would be displaying a brief message to the user that the page is only intended for mobile devices, along with a link to the non-mobile version and a link to access the mobile version anyways.
You can find some User-Agent strings sent by mobile browser here: http://www.zytrax.com/tech/web/mobile_ids.html
User-Agent header can be queried through the $_SERVER pseudo-variable: http://php.net/manual/en/reserved.variables.server.php
Edit after update of question:
Probably you should not rely on the url endpoint being accessed from you app only. One method of restricting access is configuring you app to send a specific header, which may be, and in fact, if there is someone out there who really wants it, will be extracted from your app one day. Probably a combination of a custom User-Agent string and a custom header like X-My-App-Name (invent some smart name) will be enough for most practical purposes. The server could read these headers, and if they are not present, display a warning to the user along the app download link.
How should the custom headers be send, this largely depends on what platform do you develop. For example, if the package org.apache.http.clint is available (for example, on Android), then the method org.apache.http.HttpMessage.addHeader(String name, String value) is the easiest way.
Some documentation on org.apache.http.clint is available, for example, here: http://developer.android.com/reference/org/apache/http/HttpMessage.html
what In think you're looking for is the client's user agent. I know nothing about php but it looks like $_SERVER['HTTP_USER_AGENT']; could help you.
maybe it's better to allow all mobile user agents instead of blocking all other user agents because in the future other browsers are goint to be released and you'll have a bug. for instance IE9 is being release in a few weeks.
Hope it helped, Cheers
<?php
$headers = '';
foreach ($_SERVER as $key => $value) {
if (strpos($key, 'HTTP_') === 0 && $key != 'HTTP_HOST' && $key != 'HTTP_CONNECTION') {
$key = strtolower(strtr(substr($key, 5), '_', '-'));
$headers .= $key . ': ' . $value . "\r\n";
}
}
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=> $headers,
)
);
$data = file_get_contents('http://phd.yandex.net/detect', false, stream_context_create($opts));
if($data != '<yandex-mobile-info-error>Unknown user agent and wap profile</yandex-mobile-info-error>')
{
//your code
}
else
{
echo 'these url accesible only from mobile';
}
?>
Full documentation for yandex phd you can find at http://api.yandex.ru/detector/doc/dg/concepts/detector-request.xml (translate it from russian with google translator)
You can also use WURLF to get more infos about the agent : http://wurfl.sourceforge.net/index.php (they have a PHP api)
精彩评论