Whatever you echo in PHP it will be received as HTTP response. we have already discussed about the same in this post. sending a response from a php script to iOS ,
i suggest you build an array of data on whatever information you want to send back to your iPhone. and then convert the array to json using PHP's json_encode() function. this way you will avoid from unnecessary data being sent.
Update: you can implement it by making use of conditional statement.
Method 1 : Detect if the request is coming from an iPhone directly using PHP's agent detection function.
function isIphone($user_agent=NULL) {
if(!isset($user_agent)) {
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
}
return (strpos($user_agent, 'iPhone') !== FALSE);
}
if(isIphone()) {
//the query is coming from iPhone, place your data you want to send back to iphone here
} else {
//query is not from iPhone, you can place rest of your code here
}
alternatively for this method you can use javascript to detect the iPhone, i suggest you use PHP because if someone disables javascript then it can result into unexpected behavior by your code, on how to do this using Javascript have a look at the below link(the above example is taken from the same link).
Detect iPhone Browser
Method 2: When you query from your iPhone. send some HTTP post value telling PHP that the request is coming from an iPhone. IMO it is better to use POST
then GET
because someone might come to know about your URL pattern and might try tweaking it. you can workaround this by sending another random value from iPhone and checking it in server side.
so instead of this
http://mysite.com?ua=iphone
you can try sending this
http://mysite.com?ua=iphone&ran=jkdkfjklj678
and then in PHP you can check it using
if(isset($_POST['ua']) && $_POST['ua'] == 'iPhone') {
//The request is coming from iPhone, make sure to check the random value too
} else {
//The request is not from iPhone, render the other page.
}
Method 3:
why not simply send the request from iPhone to another PHP script instead of the same page? this way you could separate both the logic and avoid messing up with script.
for example.
For iPhone you can use iphone.php
and for the rest somepage.php
, to me this approach is lot more cleaner.