Posts Tagged ‘php’

properties & pass variable form to php

March 22nd, 2016

access private properties and pass
variable form to php part 2

The part one shown you guys about how to use extract method for converted

all post which submit by HTML form to array.

In this part I would present that bring variable from HTML

from with method=”POST” to list in

array and access private properties via method.

and you can see the new style of generated $_REQUEST:

function setvalue($key) {
if(isset($_REQUEST[$key])) echo $_REQUEST[$key];
}

if($_SERVER[‘REQUEST_METHOD’] == “POST”){
extract($_POST);

You can check below code for detail:

Cal1.php


<?php
class Cal1{

private $x;
private $y;

function __construct($a = NULL ,$b = NULL){
$this->x = $a;
$this->y = $b;
}

function sum() {
$sum = $this->x + $this->y;
return $sum;
}

function sub() {
return $this->x - $this->y;
}

function div() {
return $this->x / $this->y;
}

function mul() {
return $this->x * $this->y;
}

}
?>

Viewcal5.php

<?php
include ('libs/Cal1.php');
function setvalue($key) {
if(isset($_REQUEST[$key])) echo $_REQUEST[$key];
}
$result ="";
if($_SERVER['REQUEST_METHOD'] == "POST"){
extract($_POST);
$obj = new Cal1($a,$b);
$opt = end($_POST);
$o = array_search($opt,$_POST);
$result = $obj->{$o}();
}
?>
<!DOCTYPE html>
<head>
<title>Access to private properties</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
A: <input type="text" name="a" value="<?php setvalue("a");?>"><br />
B: <input type="text" name="b" value="<?php setvalue("b"); ?>"><br />
Result: <input type="text" value="<?php echo $result;?>" />
<input type="submit" name="sum" value="+" />
<input type="submit" name="sub" value="-" />
<input type="submit" name="mul" value="*" />
<input type="submit" name="div" value="/" />
</form>
</body>

access private properties and pass variable form to php part 1

March 17th, 2016

access private properties and pass variable form to php part 1

Below code is how to access private class by its method and pass through variable in HTML form to PHP code.

First we need coding class and assign the name of the file called Cal1.php.

Second we create HTML from with some variables and try to pass its to php code using extract() method and assign the file called viewcal1.php.

  • extract() method is the method that abstract the global variable $_POST after submit to array key in php.

Finally, you can test your code

Cal1.php

Cal1.php

 

 

 

 

 

 

 

 

 

 

viewcal1

viewcal1

Register and Verify to Database with PHP & MySQL

January 28th, 2011

this code is not yet completed. But it is very helpful with your assignment.

registration.php


<?php
session_start();
include("include/config.php");
$btn_submit = $_POST['btnsubmit'];
$txt_fname = $_POST['txtfname'];
$txt_lname = $_POST['txtlname'];
$txt_user    = $_POST['txtuser'];
$txt_pass    = $_POST['txtpassword'];
$txt_email = $_POST['txtemail'];
$txt_reemail = $_POST['txtreemail'];
$gender      = $_POST['gender'];
$txt_month   = $_POST['txtmonth'];
$txt_date    = $_POST['txtdate'];
$txt_year    = $_POST['txtyear'];
$code = session_id();

$validemail="^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{1,})*\.([a-z]{2,}){1}$";

if(isset($btn_submit)){
$check_verify_email = mysql_query("SELECT * FROM tbluser_verify WHERE email='".$txt_email."'");
$check_email = mysql_query("SELECT * FROM tbluser WHERE email='".$txt_email."'");
if(!mysql_num_rows($check_verify_email) && !mysql_num_rows($check_user)){
$insert_user = mysql_query("INSERT INTO tbluser (fname,lname,email,username,password,gender,month,date,year,active) VALUES ('".$txt_fname."','".$txt_lname."','".$txt_email."','".$txt_user."','".md5($txt_pass)."','".$gender."','".$txt_month."','".$txt_date."','".$txt_year."','0')",$cnn);
$insert_user_verify = mysql_query("INSERT INTO tbluser_verify (email,code) VALUES('".$txt_email."','".$code."')",$cnn);
echo "Your Registration is complete Please see the link for activate<br>";
//echo "http://www.pisethnov.co.cc/active.php?email=$txt_email&&code=$code";
echo "active.php?email=$txt_email&&code=$code";
session_destroy();
}
else{
echo "Email Already Exist in Database.";
}

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>User Registration</title>
</head>
<form action="#" method="post">
<table>
<tr><td colspan="3"><h2>User Registration Form</h2></td></tr>
<tr><td>First Name:</td><td colspan="2"><input type="text" name="txtfname" /></td></tr>
<tr><td>Last Name:</td><td><input type="text" name="txtlname" /></td></tr>
<tr><td>Your Email:</td><td><input type="text" name="txtemail" /></td></tr>
<tr><td>Re-Enter Email:</td><td><input type="text" name="txtreemail" /></td></tr>
<tr><td>Username:</td><td><input type="text" name="txtuser" /></td></tr>
<tr><td>Password:</td><td><input type="password" name="txtpassword" /></td></tr>
<tr>
<td>I am:</td>
<td>
<select name="gender">
<option value="0">Select Gender:</option>
<option value="1">Female</option>
<option value="2">Male</option>
</select>
</td>
</tr>
<tr>
<td>Birthday:</td>
<td>
<input type="text" size="3" name="txtmonth" value="month" />
<input type="text" size="2" name="txtdate" value="date" />
<input type="text" size="4" name="txtyear" value="year"/>
</td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="btnsubmit" value="Register"</td>
</tr>
</table>
</form>
<body>
</body>
</html>

active.php

<?php
include("include/config.php");
if(isset($_GET['email'])&& isset($_GET['code'])){
$check_user_verify= mysql_query("SELECT * FROM tbluser_verify WHERE code='".$_GET['code']."' AND email='".$_GET['email']."'",$cnn);
if(mysql_num_rows($check_user_verify)){
$check_user = mysql_query("SELECT * FROM tbluser WHERE email='".$_GET['email']."'",$cnn);
if(mysql_num_rows($check_user)){
$ac="1";
//"UPDATE tblstudent SET gender='Male' WHERE studid='1'",$connect);
$insert = mysql_query("UPDATE tbluser SET active='".$ac."' WHERE email='".$_GET['email']."'");
echo "Welcome to the hell of PHP code...";
$delete = mysql_query("DELETE * FROM tbluser_verify WHERE code='".$_GET['code']."'");
}
}
}
?>

Using Session in PHP

January 27th, 2011

startsession.php

<?php
session_start();
$sessvar = "dara";
$_SESSION['var']= $sessvar;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo session_id();?></title>
</head>
<body>
</body>
</html>

viewpage.php

<?php
session_start();
if($_SESSION['var'] == "dara"){
echo "You have rights to visit this page.";
}
else{
echo "You can not access this page.";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

Login a user using PHP & MySQL

January 3rd, 2011

Login user using php & mysql
This code is not yet completed but it is usable.

<?php
$user = $_POST['txtusername'];
$pass = $_POST['txtpassword'];
$login = $_POST['btnlogin'];

$dbhost = "localhost";
$dbuser = "iicuser";
$dbpass ="123456";
$dbname = "iicdatabase";

$cnn = mysql_connect($dbhost,$dbuser,$dbpass);
$sldb = mysql_select_db($dbname,$cnn);
if(isset($login)){
if(!$user){
echo "Please Input Your Username";
}
else{
$check_username = mysql_query("SELECT username FROM tbladmin WHERE username='".$user."'");
if(!mysql_num_rows($check_username)){
if(!$pass){
echo "Please Input Your Password";
}
else{
$check_password = mysql_query("SELECT password FROM tbladmin WHERE password='".$pass."'");
if(mysql_num_rows($check_password) && mysql_num_rows($check_username)){
echo "Well Done";
}
else{
echo "You might wrong username or password.";
}
}
}
}

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login with database</title>
</head>
<form action="#" method="post">
Username:<input type="text" name="txtusername" /><br />
Password:<input type="text" name="txtpassword" /><br />
<input type="submit" name="btnlogin" value="Login" />
</form>
<body>
</body>
</html>
Facebook Auto Publish Powered By : XYZScripts.com