0

I am working on an iphone project involving use of a webserver and i am getting data in form of JSON format. I have made a table "login_info" in mysql database which will store the UserID, Username and Password. On the wesite, i am getting username and password to login and display the information page.

Its ok that after login on the website i am getting other page, but to display the data of that info page in iphone i have to convert them into json format. My question is how can i achieve this, like on website i should get info page and on the iphone , i should get json array of the values on info page.

If i echo in php the json array, it will simply dump it on the website page along with other details on webpage. I was thinking that anyhow i can make use of UserID here after login and on the basis of that UserID i can get details of other webpages if i add more webpages. Username and Password will just be used for login purpose, returning UserID and later data will be accessed using that UserID. Please guide me as am not experienced in web part and trying to learn.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
iSim
  • 303
  • 2
  • 3
  • 9

1 Answers1

0

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.

Community
  • 1
  • 1
Ibrahim Azhar Armar
  • 25,288
  • 35
  • 131
  • 207
  • if you are unable to understand on how to do this. let me know and ill explain. – Ibrahim Azhar Armar Feb 24 '12 at 18:58
  • Hi Azhar, thanks for the reply. I have converted the data on the info page into json array and have accessed it successfully in iphone, but what i want is that i dont want to display that json array on webpage after login, instead i want the actual webpage wher user can see details like on normal webpage. – iSim Feb 24 '12 at 19:24
  • Hi Azhar, Thank you very much. you provided me with good ideas. – iSim Feb 25 '12 at 19:34