Developer Forums relocated!

Need help with Apple Developer tools and technologies? Want to share information with other developers and Apple engineers? Visit Developer Forums at Apple.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

(Xcode) How to connect to a PHP page?

I want to login at a page with my username and password. I want that my app connects to the php page in a UIWebView and login.

iPhone 4, iOS 4

Posted on May 12, 2011 8:26 AM

Reply
33 replies

May 20, 2011 5:50 AM in response to G_Morales

The people helping you get points when you mark their responses as helpful or solved (I see you have already done that). More than points to the responder, marking helps other users searching the forum for help as it lets them know which of the responses were useful to the original poster.


As long as the page you are trying to access is available on the web, the same process should work. From your perspective, you are only dealing with a HTML form handler. The handler may be implemented in any technology (PHP, Ruby, Python, .NET, Java, C/C++, PERL...)

May 20, 2011 1:37 PM in response to sptrakesh

And how do I do that? I changed the sa,e things you told me the web page and the name that the handler expects (in the source code of the page as you told me) but nothing happens


NSString *post = [[NSStringalloc] initWithFormat:@"Login=%@&password=%@",nameInput.text,passInput.text];


NSData *postData = [post dataUsingEncoding:NSASCIIStringEncodingallowLossyConversion:YES];


NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];


NSURL *url=[NSURLURLWithString:@"http://www.info-upsite.upmx.mx/selfservice/Login.asp"];

NSMutableURLRequest *theRequest = [NSMutableURLRequestrequestWithURL:url];

[theRequest setHTTPMethod:@"POST"];

[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];

[theRequest setHTTPBody:postData];

May 20, 2011 2:20 PM in response to G_Morales

Look at the <form name="Login" action="xxx" in the source. You will need to specify the query string as well in your request. You can either use the URL as specified in the action or just add the parameter as part of your post body. Usually form handlers do not distinguish between URL query strings and post data.


Also, looking at the source, the username text field is named login and not Login.

May 20, 2011 3:20 PM in response to sptrakesh

Mmmm... ok! So i have to include the action of the login in my post?


Im my source it appears like this:


<form name="Login" action="Login.asp?ret_link=%2Fselfservice%2FDefault%2Easp&type=notLogged&ccsForm=Login" method="post">


login with capital L


and muy post how should it look? like this?


NSString *post = [[NSStringalloc] initWithFormat:@"Login.asp?ret_link=%2Fselfservice%2FDefault%2Easp&type=notLogged&ccsForm=Login =%@&password=%@",nameInput.text,passInput.text];


May 21, 2011 7:10 PM in response to G_Morales

NSString *post = [[NSStringalloc]initWithFormat:@"ccsForm=Login&login=%@&password=%@",nameInput.text,passInput.text];


NSData *postData = [post dataUsingEncoding:NSASCIIStringEncodingallowLossyConversion:YES];


NSString *postLength = [NSString stringWithFormat:@"%d", [postDatalength]];


NSURL *url=[NSURLURLWithString:@"http://www.info-upsite.upmx.mx/selfservice/Login.asp"];

NSMutableURLRequest *theRequest = [NSMutableURLRequestrequestWithURL:url];

[theRequest setHTTPMethod:@"POST"];

[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];

[theRequest setHTTPBody:postData];

May 22, 2011 3:26 AM in response to sptrakesh

It didn't worked man! Also i don´t know what happened!! I tried to login to the php page (the one i asked you at the start) and it doesn`t works again! This is a college proyect I have to finish. Can I send you my proyect to your mail or something like that and could you check it for the two connections? I don´t know what happened.


Sorry for everything

May 22, 2011 4:06 PM in response to G_Morales

I do not see anything really wrong with the way you are communicating with the form handler on the server (you may have to send additional parameters are part of your post body, eg. the <input type="hidden" name="xxx" value="xxx"> items).


The bigger question is what exactly do you hope to achieve with this application? Even if you successfully log in, there is not guarantee that subsequent background requests will succeed. If you want those to succeed, you will need to send over session information (cookies, session id encoded in url etc) as part of subsequent requests to the server.

May 22, 2011 8:22 PM in response to G_Morales

Okay, there are two options for you, one that is complex (one that I have not tried, so cannot say how successful it will be), and the other that is very simple (although may not satisfy your project requirements)


1) The hard way. Get just the form post and authentication working. Once that is done, try and open the resulting HTML content in a UIWebView. This should load, but subsequent clicking on links will probably lack cookie information (that is sent as part of the response headers, which you can store and try to use later). In the web view delegate methods, attempt to inject the cookie and other header information into the request. This will work only if the request instance you are sent is a mutable instance. If not, I am afraid you are out of luck, unless your web server works in some other way that does not require cookies, and encodes the session information into each link on the page (unlikely).

2) The easy way. Create a local HTML file resouce that is designed the way you want and that will post to the form handler URL that you desire. Load this file resource in UIWebView, and everything will work as though you were using Safari.

(Xcode) How to connect to a PHP page?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.