Intergrate Python with PHP - RMB Scripting
You appear not have have an account with us, Click HERE to register an account...
RMB Scripting
Navigation
  Home
  Contact
  FAQ
  Forum
  Topsites
  Members
  Affiliates
  Arcade <-- New!
  IoxHost
Downloads
  Templates
  Scripts
  Fonts
  Image Sets
  Software
  Other
 
Tutorials
  .htaccess
  Photoshop
  C/C++
  CSS
  Macromedia Flash
  MySQL
  PHP
  PHP - User System
  HTML
  Javascript
  Macintosh
  Macromedia Fireworks
  Visual Basic
  Visual C#
  Windows
  Python
  Other
 
Latest Tutorials
All PHP / Apache Funct...
Block User\'s Direct A...
Generate a salt
Image Tag Tracking
Cpanel/webmail Login
Latest Comments
Friendlist/Blocklist +...
Friendlist/Blocklist +...
Friendlist/Blocklist +...
Steal msn picture
Simple and minimalisti...
Stats
Total Members: 562 [216]
Total Tutorials: 269
Newsest User: Stf
Todays Unique Hits: 49
Users Online: 0
 
6 Guests
Poll
Should we change our name?
Hmmmm, Yes!!
Maybe
Depends on what to
NO!
Main Content
Tutorial: Intergrate Python with PHP By ShadowMage
Since my friend and I decided to make a remote administration thing for our site I though I would release this tutorial.

First off we will want a class for our functions to handle data passed from the python script. I'll use one I made.

Call his pythonHandler.php or something you want.

There will be a breakdown after.

   
CODE:
<?php
class pythonHandler{//start the class
private $user; //plaintext username
private $pass; //plaintext password.
private $getUserQuery; //the get user query from teh checkUser() funcion
public function dbConn(){ //function to connect to the database.
$link = mysql_connect("localhost","Your User","Your Pass") or die(mysql_error());
mysql_select_db("Your DB", $link) or die(mysql_error());
}
public function doLogin($user, $pass){ //lets login now!
$this->user = $user; //set the user for later use
$this->pass = $pass; //set the pass for later use
if(empty($user)){ //no user
return "No User Specified"; //tell the user.
}else{ //user found
if($this->checkUser() == TRUE){ //check the user and see if its true
if(empty($pass)){ //theres no password
return "No Pass Specified";
}else{//there is a password
if($this->checkPass() == TRUE){ //verify the password is right
return "Login Successful"; //success!
}else{ //nope...made a booboo
return "Invalid Pass Specified"; //invalid password
}//end password check
}//end empty pass check
}else{ //user invalid
return "Invalid User Specified"; //Uh-oh!
}//end user invalid check
}//end user check
}//end doLogin function
private function checkUser(){ //the check user function
$this->getUserQuery = mysql_query("SELECT * FROM `members` WHERE `username` = '$this->user';"); //get the user from the database
if(mysql_num_rows($this->getUserQuery) == 0){ //is it there?
return FALSE; //no, no the user isn't there.
}else{//maybe it is...
return TRUE; //yes it is! :)
} //end the get user check
} //end the checkUser function
private function checkPass(){ //check the password!
$arrayUser = mysql_fetch_object($this->getUserQuery); //array the data for use
$userPass = $this->Encrypt($this->pass, $arrayUser->salt); //encrypt the plain text password to compare
if($userPass != $arrayUser->password){ //lets compare!
return FALSE;//not right :(
}else{ //maybe.
return TRUE; //Yes, it is right!
}//end the check.
}//end the checkPass function
public function handlerStats(){ //some stats.
return "Handler Version: 0.2\nHandler Creator: ShadowMage"; //yes I did make this :)
}//end handlerStats
function Encrypt($pw, $salt = null) //lets salt and encrypt the password
{
$pw = str_replace(array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"), "", $pw); //get rid of the numbers
$hash_a = hash("md4", "Hey Look a " . $pw . " Password " . $salt .
" Hash"); //md4 the password
$hash_b = hash("md5", $hash_a); //add a little md5
$hash_c = hash("sha1", $hash_b); //with some sha1
$hash_d = hash("sha512", $hash_c); //and sha512
$return_a = strtoupper(strrev($hash_d)); //Lets uppercase it all then reverse it now
return md5(hash("haval256,5", sha1($pw) . hash("haval256,5", $return_a) . sha1($salt))); //then output a regular md5 password with some sha1 and haval in it!
}//end the encrypt function
}//end the class.
?>


Bamn! so that's our class, I bet your thinking "Well......I'm lost...." If you are, no worries. Here is a breakdown.

   
CODE:
private $user; //plaintext username
private $pass; //plaintext password.
private $getUserQuery; //the get user query from teh checkUser() funcion

This code block here is our default variables. $user $pass and $getUserQuery. they the main three variables needed in this script.
[code=php] public function dbConn(){ //function to connect to the database.
$link = mysql_connect("localhost","Your User","Your Pass") or die(mysql_error());
mysql_select_db("Your DB", $link);
}
This block of code will connect your class to the database where information is stored.

   
CODE:
public function doLogin($user, $pass){ //lets login now!
$this->user = $user; //set the user for later use
$this->pass = $pass; //set the pass for later use
if(empty($user)){ //no user
return "No User Specified"; //tell the user.
}else{ //user found
if($this->checkUser() == TRUE){ //check the user and see if its true
if(empty($pass)){ //theres no password
return "No Pass Specified";
}else{//there is a password
if($this->checkPass() == TRUE){ //verify the password is right
return "Login Successful"; //success!
}else{ //nope...made a booboo
return "Invalid Pass Specified"; //invalid password
}//end password check
}//end empty pass check
}else{ //user invalid
return "Invalid User Specified"; //Uh-oh!
}//end user invalid check
}//end user check
}//end doLogin function

This block of code, which is the doLogin function will log in the user and return statuses for the python script to interpret.

   
CODE:
private function checkUser(){ //the check user function
$this->getUserQuery = mysql_query("SELECT * FROM `members` WHERE `username` = '$this->user';"); //get the user from the database
if(mysql_num_rows($this->getUserQuery) == 0){ //is it there?
return FALSE; //no, no the user isn't there.
}else{//maybe it is...
return TRUE; //yes it is! :)
} //end the get user check
} //end the checkUser function

This small, yet useful block of code will check if the user exists in the database and set our final variable which was not set before. The other two main variables were set at the start of doLogin().

   
CODE:
private function checkPass(){ //check the password!
$arrayUser = mysql_fetch_object($this->getUserQuery); //array the data for use
$userPass = $this->Encrypt($this->pass, $arrayUser->salt); //encrypt the plain text password to compare
if($userPass != $arrayUser->password){ //lets compare!
return FALSE;//not right :(
}else{ //maybe.
return TRUE; //Yes, it is right!
}//end the check.
}//end the checkPass function

This block of code will check, and encrypt the plain text password to compare to the password stored in the database.

   
CODE:
function Encrypt($pw, $salt = null) //lets salt and encrypt the password
{
$pw = str_replace(array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"), "", $pw); //get rid of the numbers
$hash_a = hash("md4", "Hey Look a " . $pw . " Password " . $salt .
" Hash"); //md4 the password
$hash_b = hash("md5", $hash_a); //add a little md5
$hash_c = hash("sha1", $hash_b); //with some sha1
$hash_d = hash("sha512", $hash_c); //and sha512
$return_a = strtoupper(strrev($hash_d)); //Lets uppercase it all then reverse it now
return md5(hash("haval256,5", sha1($pw) . hash("haval256,5", $return_a) . sha1($salt))); //then output a regular md5 password with some sha1 and haval in it!
}//end the encrypt function

This final block of code is the encrypt function which will take the password and a salt and combine them into one solid hash. I use this on Coding Dev but with some different words where it has "Hey Look a".

Now that we have this all done we can start the python code. This code is very basic but you may use Boa Constructor and this tutorial to make a GUI based application to log into your site remotely.
   
CODE:
import urllib #only one library this round

def Menu(): #this is our main menu with a list of options
print "1) Sign in remotely" #log into the site remotely, just a test.
print "2) Handler Stats" #get the handler status
print "3) Exit Program" #exit the script
return input ("Choose your option: ") #now, lets wait for input!

def newline(): #this is the new line function
print "" #we don't print anything ;)


loop = 1 #set the loop to one so the program will stay running
choice = 0 #the current choice is 0
while loop == 1: #begin program while loop is 1
choice = Menu() #set teh choice to teh input from the menu function
if choice == 1: #check choice and see if they wish to sign in
newline() #place a new line
openData = urllib.urlopen("http://YOURSITE.com/pythonAccess.php?act=login&user=YOURUSER&pass=YOURPASS") #open the site to the python handler with your details.
returnData = openData.read() #read the data
if returnData == "No User Specified": #see if there is a user
print "No User Specified" #no user :(
elif returnData == "Invalid User Specified": #invalid?
print "Invalid User Specified" #yes it is invalid
elif returnData == "No Pass Specified": #any password?
print "No Pass Specified" #no there wasn't a password
elif returnData == "Invalid Pass Specified": #invalid pass?
print "Invalid Pass Specified" #definately invalid!
elif returnData == "Login Successful": #Hey Look!
print "Login Successful" #Success!
openData.close() #close the data handle.
newline() #place a new line! =]
elif choice == 2: #see if they want teh handler stats
newline() #make a new line
openData = urllib.urlopen("http://YOURSITE.com/pythonAccess.php?act=handlerStats") #open the python PHP handler.
returnData = openData.read() #get the returned data
print returnData #and echo it out.
openData.close() #close the data connection
newline() #New line now!
elif choice == 3: #lets exit
loop = 0 #set loop to 0 and script will close.
else: #Invalid option
newline() #New line
print "Incorrect Input, Please Try Again" #your input was invalid
newline() #new line.


And now for a script breakdown once more!

   
CODE:
import urllib #only one library this round

def Menu(): #this is our main menu with a list of options
print "1) Sign in remotely" #log into the site remotely, just a test.
print "2) Handler Stats" #get the handler status
print "3) Exit Program" #exit the script
return input ("Choose your option: ") #now, lets wait for input!

def newline(): #this is the new line function
print "" #we don't print anything ;)

This block of code imports the url library which makes it possible to access outbound URLs such as google.com. This block also declares our two main functions for the main menu, and newline.

   
CODE:
loop = 1 #set the loop to one so the program will stay running
choice = 0 #the current choice is 0
while loop == 1: #begin program while loop is 1
choice = Menu() #set teh choice to teh input from the menu function

This block of code sets the loop to 1 so that the program will remain running in the while loop and set our choice to the input from the user at teh main menu.

Now, for the final block of code.
   
CODE:
if choice == 1: #check choice and see if they wish to sign in
newline() #place a new line
openData = urllib.urlopen("http://YOURSITE.com/pythonAccess.php?act=login&user=YOURUSER&pass=YOURPASS") #open the site to the python handler with your details.
returnData = openData.read() #read the data
if returnData == "No User Specified": #see if there is a user
print "No User Specified" #no user :(
elif returnData == "Invalid User Specified": #invalid?
print "Invalid User Specified" #yes it is invalid
elif returnData == "No Pass Specified": #any password?
print "No Pass Specified" #no there wasn't a password
elif returnData == "Invalid Pass Specified": #invalid pass?
print "Invalid Pass Specified" #definately invalid!
elif returnData == "Login Successful": #Hey Look!
print "Login Successful" #Success!
openData.close() #close the data handle.
newline() #place a new line! =]
elif choice == 2: #see if they want teh handler stats
newline() #make a new line
openData = urllib.urlopen("http://YOURSITE.com/pythonAccess.php?act=handlerStats") #open the python PHP handler.
returnData = openData.read() #get the returned data
print returnData #and echo it out.
openData.close() #close the data connection
newline() #New line now!
elif choice == 3: #lets exit
loop = 0 #set loop to 0 and script will close.
else: #Invalid option
newline() #New line
print "Incorrect Input, Please Try Again" #your input was invalid
newline() #new line.

This final, big block of code will check the choice given at the main menu and do what the choice number is. Such as 1 will log you into the site and return a status for processing, or 2 to get the handler stats.

Now that we have our script, and python handler we will want to make the pythonAccess.php file.
So, here we go!

Make a new file first off and name it pythonAccess.php

Now to populate the file.
   
CODE:
<?php
require("pythonHandler.php"); #get our pythonHandler class
$handler = new pythonHandler(); #declare we want a new class
$handler->dbConn(); #connect to the database
switch($_GET['act']){ #multi-pages for the
default: #default page, no action default or invalid action.
die("Invalid Action Link"); #The user made a booboo, or the script rather failed.
break; #end page
case 'login': #lets login!
print $handler->doLogin($_GET['user'], $_GET['pass']); #get the returned output from the handler with the user and pass given in the python script.
break; #end the login page.
case 'handlerStats': #get the handler status
print $handler->handlerStats(); #print it out for the program to interpret.
break; #end the page
} #end the multipage.
?>


Note: The python script MUST have a blank line, no tabs at the end of the file or it will not work.

Hope you learned something!
Difficulty: Easy
Views: 324
Rating:
Comments
There are currently no comments for this tutorial.
Notice:
Remember to post long codes on our pastbin! - (http://rmb.pastebin.com/)
Add Comment
You must be logged in to post a comment.
Good Morning Guest
Username: 
Password: 
Remember Username
Links
RMB Arcade
$2.50 Resellers
Free Domains
Free Games
Affiliates