Hello. I'm new here. I'm just started learning PHP SOAP.
I am trying to to create a simple user registeration and login form.
The login and register function works well.
My idea is to make the soap persistence. After login for the 1st time, I would like to set my status as "logged in" for visiting "http://hostc.localhost/login.php" for next time.
Is it anything to do with session? or the object defined in class? Is there any way that enhance my code so that I still "logged in" when I go to "login.php" for the second time?
Please give me some advise. Thank you.
Below are my codes.
login.html
<html>
<title>Login</title>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<h1>WELCOME</h1>
<p>LOGIN</p>
<form action="login.php" method="POST">
<table border=1 bgcolor=#FFFFFF >
<tr><td>Username:</td><td><input type="text" name="loginusername" /></td></tr>
<tr><td>Password:</td><td><input type="password" name="loginpassword" /></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="submit" value="LOGIN"/></td></tr>
</table>
</form>
<a href="http://hostc.localhost/register.html">REGISTER HERE</a>
</html>
login.php
<html>
<head>
<title>Login Page</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<?php
session_start();
ini_set("soap.wsdl_cache_enabled", 0);
$lclient = new soapClient("http://hostb.localhost/record.wsdl");
$lusername=$_POST['loginusername'];
$lpassword=base64_encode($_POST['loginpassword']);
$loutput=$lclient->loginUser($lusername,$lpassword);
if (is_soap_fault($loutput))
{
trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
}
else
{
echo "RESULT: <p>$loutput</p>";
}
?>
</html>
register.html
<html>
<title>Register</title>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<h1>WELCOME</h1>
<p>REGISTER</p>
<form action="register.php" method="POST">
<table border=1 bgcolor=#FFFFFF >
<tr><td>Username:</td><td><input type="text" name="registerusername" /></td></tr>
<tr><td>Password:</td><td><input type="password" name="registerpassword" /></td></tr> <tr><td colspan="2" align="center"><input type="submit" value="REGISTER"/></td></tr>
</table>
</form>
<a href="http://hostc.localhost/login.html">LOGIN HERE</a>
</html>
register.php
<html>
<title>Register Page</title>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<?php
session_start();
ini_set("soap.wsdl_cache_enabled", 0);
$rclient = new soapClient("http://hostb.localhost/record.wsdl");
$rusername=$_POST['registerusername'];
$rpassword=base64_encode($_POST['registerpassword']);
$routput=$rclient->registerUser($rusername,$rpassword);
if (is_soap_fault($routput))
{
trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
}
else
{
echo "RESULT: <p>$routput</p>";
}
?>
</html>
server2.php
<?php
function connection()
{
define("DB_SERVER","localhost");
define("DB_USER","root");
define("DB_PASSWORD","12345");
define("DB_NAME","test1");
//1. Create a database connectivity
$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASSWORD);
if(!$connection)
{
die("Database connection failed" . mysql_error());
}
//2. Select the database from the server
$db_select= mysql_select_db(DB_NAME,$connection);
if(!$db_select)
{
die("Database selection failed " . mysql_error());
}
}
class test01{
public function registerUser($rusername,$rpassword)
{
$this->rusername=$rusername;
$this->rpassword=$rpassword;
if(empty($this->rusername)||empty($this->rpassword))
{
$output="Invalid registeration!
<p><a href=http://hostc.localhost/register.html> REGISTER HERE </a></p>";
return $output;
}
connection();
$query = "SELECT username FROM information WHERE username='$this->rusername'";
$result = mysql_query($query);
$count=mysql_num_rows($result);
$sql="INSERT INTO information (username, password) VALUES('$this->rusername','$this->rpassword')";
if($count!=0)
{
$routput="Sorry, the username is already taken, please pick another one.
<p><a href=http://hostc.localhost/register.html> REGISTER HERE </a></p>";
return $routput;
}
else
{
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
else
{
$routput="You are registered successfully.
<p><a href=http://hostc.localhost/login.html>LOGIN HERE</a></p>";
return $routput;
}
}
}
public function loginUser($lusername,$lpassword)
{
$this->lusername=$lusername;
$this->lpassword=$lpassword;
if(empty($this->lusername)||empty($this->lpassword))
{
$output="Invalid login!
<p><a href=http://hostc.localhost/login.html> LOGIN HERE </a></p>";
return $output;
}
connection();
/* Check user details */
$query1 = "SELECT status FROM information WHERE username='$this->lusername' AND password='$this->lpassword' and status='0'";
$query2 = "SELECT status FROM information WHERE username='$this->lusername' AND password='$this->lpassword' and status='1'";
$result1 = mysql_query($query1);
$result2 = mysql_query($query2);
if(mysql_num_rows($result1)==1)
{
$_SESSION['username']=$this->lusername;
$loutput= "<p>You are PUBLIC user. </p>
<p><a href=http://hostc.localhost/public.php>Click here to view only public user list</a></p>";
return $loutput;
}
if(mysql_num_rows($result2)==1)
{
$_SESSION['username']=$this->lusername;
$loutput= "<p>You are a PRIVATE user. </p>
<p><a href=http://hostc.localhost/private.php>Click here to view all user list</a></p>";
return $loutput;
}
else
{
$loutput= "Sorry,wrong username and password.
<p><a href=http://hostc.localhost/login.html>LOGIN HERE</a></p>";
return $loutput;
}
}
}
//initial setting
ini_set("soap.wsdl_cache_enabled", 0); // disabling WSDL cache
ini_set("session.auto_start", 0);
//include class file
// require_once('myclass.php');
//for persistent session
session_start();
//service
$server =new SoapServer("http://hostb.localhost/record.wsdl");
$server->setClass("test01");
$server->setPersistence(SOAP_PERSISTENCE_SESSION);
$server->handle();
?>
and record.wsdl
<?xml version ='1.0' encoding ='UTF-8'?>
<definitions name='Record'
targetNamespace='http://hostb.localhost/record.wsdl'
xmlns:tns='http://hostb.localhost/record.wsdl'
xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
xmlns='http://schemas.xmlsoap.org/wsdl/'>
<message name='registerUserRequest'>
<part name='registerusername' type='xsd:string'/>
<part name='registerpassword' type='xsd:string'/>
</message>
<message name='registerUserResponse'>
<part name='Result' type='xsd:string'/>
</message>
<message name='loginUserRequest'>
<part name='loginusername' type='xsd:string'/>
<part name='loginpassword' type='xsd:string'/>
</message>
<message name='loginUserResponse'>
<part name='Result' type='xsd:string'/>
</message>
<portType name='RecordPortType'>
<operation name='loginUser'>
<input message='tns:loginUserRequest'/>
<output message='tns:loginUserResponse'/>
</operation>
<operation name='registerUser'>
<input message='tns:registerUserRequest'/>
<output message='tns:registerUserResponse'/>
</operation>
</portType>
<binding name='RecordBinding' type='tns:RecordPortType'>
<soap:binding style='rpc'
transport='http://schemas.xmlsoap.org/soap/http'/>
<operation name='registerUser'>
<soap:operation soapAction='urn:localhost-record#registerUser'/>
<input>
<soap:body use='encoded' namespace='urn:localhost-record'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
</input>
<output>
<soap:body use='encoded' namespace='urn:localhost-record'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
</output>
</operation>
<operation name='loginUser'>
<soap:operation soapAction='urn:localhost-record#loginUser'/>
<input>
<soap:body use='encoded' namespace='urn:localhost-record'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
</input>
<output>
<soap:body use='encoded' namespace='urn:localhost-record'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
</output>
</operation>
</binding>
<service name='RecordService'>
<port name='RecordPort' binding='RecordBinding'>
<soap:address location='http://hostb.localhost/server2.php'/>
</port>
</service>
</definitions>