When you join a forum you have to define a username and type in your email address and a password. After that you receive an email to finish the registration. Today I want to show the process how to implement this with b4a and php.
Workflow
The MySqlTable
It's a very simple table with the following culumns
name (the username)
mail (email address)
t (timestamp, with this we can add some functions like automatic logoff, etc.)
regnr (the random number will be stored here)
status (is used in the different stages of registration like "mail is sent" or "registered")
pw (password)
online (online status)
The app
Create 3 Edittext Views (name, mail address and password) and 3 buttons (register, login, logout)
REGISTER
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
Here we just start a httpjob with the parameters name, mail address and password and send it to the script
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
First it checks if the mail address is in use. If not, an email is sent with a random number.
To The app we will send back a single value "Mail" or "MailInUse". So we can check what thr script did:
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
The user will be informed if the Mail is in use or if he got an email
The email looks like:
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
As we know there is no difference calling a php scrip by our app via httputils2 or by typing it directly in the browser (clicking on the link does the same)
In the script:
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
The status is first set to "M" (=mail sent) to "R" (= registered) when the link is clicked
By using a random number we add security to the process because nobody can know the number and it will be send to the given mail address only. Typing the link in the browser without knowing the number will not work
After that I have added a login sub in my app and added the counterpart in the php in the same way.
			
			Workflow
- User types in username, mail address and password
- The data will be sent to a php script
- The script will check if the mail address is already in use
- Otherwise it will insert the data in a table with a random number generated
- An email will be sent to the user containing this random number with a link to click on
- Clicking on the link the php script will be called again
- The script checks if the mail adress and random number matches
- If yes, it will update the table and the user is registered
- User can now login
The MySqlTable
It's a very simple table with the following culumns
name (the username)
mail (email address)
t (timestamp, with this we can add some functions like automatic logoff, etc.)
regnr (the random number will be stored here)
status (is used in the different stages of registration like "mail is sent" or "registered")
pw (password)
online (online status)
The app
Create 3 Edittext Views (name, mail address and password) and 3 buttons (register, login, logout)
REGISTER
			
				B4X:
			
		
		
		Sub RegButton_Click
     
    Dim register As HttpJob
    register.Initialize("register", Me)
    register.Download2("http://www.yourdomain.com/registerlogin.php", _
      Array As String("Action", "Register", "Name", RegName.Text, "Mail", RegMail.Text, "PW", RegPW.Text))
End SubHere we just start a httpjob with the parameters name, mail address and password and send it to the script
			
				B4X:
			
		
		
		$action = $_GET["Action"];
switch ($action) {
    case "Register":
        $mail=$_GET["Mail"];
        $name = $_GET["Name"];
        $pw= $_GET["PW"];
      
        $q = mysql_query("SELECT * FROM Users Where mail = '$mail'");
        $count = mysql_num_rows($q);
      
      
        if ($count == 0)
            {
          
            $randomnumber = mt_rand(111111, 999999);
            $res = mysql_query("Insert into Users (name, mail, regnr, status, pw, online) VALUES ('$name', '$mail', $randomnumber, 'M', '$pw', 'N')");
            mail('$mail','Your registration',
                'Please klick on this link to finish the registration process: ' .
                  'www.yourdomain.com/registerlogin.php?Action=Mail&Mail=' . $mail . '&RegNr=' . $randomnumber,
                  "From: [email protected]");
            print json_encode ("Mail");
            }          
        else
            print json_encode ("MailInUse");
      
      
        break;First it checks if the mail address is in use. If not, an email is sent with a random number.
To The app we will send back a single value "Mail" or "MailInUse". So we can check what thr script did:
			
				B4X:
			
		
		
		Sub JobDone(Job As HttpJob)
    ProgressDialogHide
    If Job.Success Then
    Dim res As String, action As String
        res = Job.GetString
        Log("Back from Job:" & Job.JobName )
        Log("Response from server: " & res)
      
        Dim parser As JSONParser
        parser.Initialize(res)
      
      
      
        Select Job.JobName
                      
            Case "register"
                action = parser.NextValue
                If action = "Mail" Then
                    Msgbox("A mail was sent to " & RegMail.Text & ". Please click on the link to finish registration", "Registration")
                    
                End If
              
                If action = "MailInUse" Then
                    Msgbox("The mail address " & RegMail.Text & " is already in use", "Registration")
                End If
...The user will be informed if the Mail is in use or if he got an email
The email looks like:
			
				B4X:
			
		
		
		Please klick on this link to finish the registration process: www.yourdomain.com/registerlogin.php?Action=Mail&[email protected]&RegNr=547057As we know there is no difference calling a php scrip by our app via httputils2 or by typing it directly in the browser (clicking on the link does the same)
In the script:
			
				B4X:
			
		
		
		$action = $_GET["Action"];
switch ($action) {
case "Mail":
        $regnr = $_GET["RegNr"];
        $mail=$_GET["Mail"];
      
        $q = mysql_query("SELECT * FROM Users Where mail = '$mail' and regnr = $regnr and status = 'M'");
        $count=mysql_num_rows($q);
      
      
      
        if ($count == 0)
            {
            print json_encode ("This registration is not valid / mail address ist already registered");
            }          
        else {
            $res=mysql_query("Update Users SET Status = 'R' where Mail = '$mail' and RegNr =$regnr");
            echo("$mail is registered now :-)");
            }
        break;The status is first set to "M" (=mail sent) to "R" (= registered) when the link is clicked
By using a random number we add security to the process because nobody can know the number and it will be send to the given mail address only. Typing the link in the browser without knowing the number will not work
After that I have added a login sub in my app and added the counterpart in the php in the same way.
			
				Last edited: 
			
		
	
							 
				 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		