سؤال

So I have a php registration script on my server that I use.

How can I take the input of text boxes (or the variables/pointers relating to the content of these boxes), and throw them in a url to register a user. Again the PHP is all set up as well as the SQL database, I'm just having trouble with it in iOS.

Example:

VARIABLE --- PURPOSE
uname        username
pass         password
name         first name
lname        last name

Now I need to throw this in a url like this

https://mywebsite/register.php?username=uname&password=pass&firstname=name&lastname=lname&sumbit=submit

The script works perfectly fine, I just need help with implementing this in iOS.

Much thanks in advance. You guys are awesome!

لا يوجد حل صحيح

نصائح أخرى

You can use NSURLConnection in combination with NSMutableURLRequest as follows:

//Prepare URL for post
NSURL *url = [NSURL URLWithString: @"https://mywebsite/register.php"];

//Prepare the string for your post (do whatever is necessary)
NSString *postString = [@"username=" stringByAppendingFormat: @"%@&password=%@", uname, pass];

//Prepare data for post
NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

//Prepare request object
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setTimeoutInterval:20];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

//send post using NSURLConnection
NSURLConnection connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

//Connection should never be nil
NSAssert(connection != nil, @"Failure to create URL connection.");

This (with the postString adapted to your needs) should send a POST request asynchronously (in the background of your app) to your server and the script on your server is run.

Assuming that your server is also returning an answer, you can listen to this as follows:
When creating the connection with NSURLConnection connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];, we told the connection that we process what the server returns within the same object, i.e. self.

When the connection returns an answer the following three methods are called (put them in the same class as the code above):

-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
    //Initialize received data object        
        self.receivedData = [NSMutableData data];
        [self.receivedData setLength:0];

    //You also might want to check if the HTTP Response is ok (no timeout, etc.) 
}

-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{     
    [self.receivedData appendData:data];
}

-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
    NSLog(@"Connection failed with err");
}

-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{
    //Connection finished loading. Processing the server answer goes here.

}

Keep in mind, that you have to handle the server authentication that comes with with your https connection/ request. To allow any server certificate, you can rely on the following solution (although this is just recommended for quick prototyping and testing): How to use NSURLConnection to connect with SSL for an untrusted cert?

Hope that helped.

Use for this, NSMutableURLRequest.

Set setHTTPMethod as GET method.

For each value define this:

[request setValue:value forHTTPHeaderField:key];

Give this a try:

url.php

<?php 
    //https://mywebsite/register.php?username=uname&password=pass&firstname=name&lastname=lname&sumbit=submit

    function make_safe_for_use($var){
        // run some validation here
        return($var);
    }


    if($_POST){
        //$url_username = $_POST['username']; // <--- this will work fine... but no validation...
        $url_username = make_safe_for_use($_POST['username']);
        $url_password = make_safe_for_use($_POST['password']);
        $url_firstname = make_safe_for_use($_POST['firstname']);
        $url_lastname = make_safe_for_use($_POST['lastname']);


        if($url_username > "" && $url_password > "" && $url_firstname > "" && $url_lastname > ""){
            // if all of the variables are set, then use them....
            $url = "register.php?username=".$url_username."&password=".$url_password."&firstname=".$url_firstname."&lastname=".$url_lastname."&sumbit=submit";
            header("Location: ".$url);
            exit(); // prevents any more code executing and redirects to the url above
        }
    }

?>
<form action="url.php" method="post">
    (Remove the value="donkeykong" part of each of the following, here for demo)<br /><br />
    Username <input type="text" name="username" id="username" value="donkeykong" /><br />
    Password <input type="password" name="password" id="password" value="cheese123" /><br />
    Firstname <input type="text" name="firstname" id="firstname" value="bruce" /><br />
    Lastname <input type="text" name="lastname" id="lastname" value="wayne" /><br />
    <input type="submit" value="go" />
</form>

register.php

<?php

    //print_r($_GET); // - for testing

    if($_GET){

        $url_username = $_GET['username'];
        $url_password = $_GET['password'];
        $url_firstname = $_GET['firstname'];
        $url_lastname = $_GET['lastname'];

        echo "username: ".$url_username."<br />";
        echo "password: ".$url_password."<br />";
        echo "firstname: ".$url_firstname."<br />";
        echo "lastname: ".$url_lastname."<br />";
    }

?>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top