user model
username}'";

$db = $this->_open_connection();

$find_user = mysql_query($select,$db);
$data_user = mysql_num_rows($find_user);

if($data_user == 0)
{

$sql = "INSERT INTO user(username,password) VALUES('{$model_user->username}','{$model_user->password}')";
$result = mysql_query($sql,$db);
return $result;
return true;

}
else
return false;
}

//LOGIN
public function user_select($model_user)
{
$select = "SELECT id FROM user WHERE username = '{$model_user->username}' AND password = '{$model_user->password}'";

$db = $this->_open_connection();

$find_user = mysql_query($select,$db);
$data_user = mysql_num_rows($find_user);
$data = mysql_fetch_array($find_user);

if($data_user == 1)
{

$_SESSION['uid'] = $data['id'];
return true;
}

else
return false;
}

//GET USER_id
public function userID($model_user)
{
$select = "SELECT id FROM user WHERE username = '{$model_user->username}'";

$db = $this->_open_connection();

$find_user = mysql_query($select,$db);
$data = mysql_fetch_array($find_user);

return $data['id'];
}

//UPDATE PROFILE
public function editProfile($model_user)
{

$sql = "UPDATE user SET name='{$model_user->name}',email='{$model_user->email}',password='{$model_user->password}',sex='{$model_user->sex}'
,city='{$model_user->city}',country='{$model_user->country}',contact='{$model_user->contact}',aboutme='{$model_user->aboutme}'
WHERE id = '{$model_user->id}'";
$db=$this->_open_connection();
$result=mysql_query($sql,$db);

$_SESSION['upload_msg'] = '';
$_SESSION['saveprofile_msg'] = "profile succesfully save";
header('Location: user.php?view=edit-profile');
}

//UPLOAD PROFILE PICTURE
public function profilepic($model_user)
{
$target_path = $_SERVER['DOCUMENT_ROOT'].'/mvc2/upload/';

$target_path = $target_path . basename( $_FILES['file']['name']);

$picname = $_FILES["file"]["name"];

if($_FILES["file"]["type"] == "image/jpeg"){
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {

$_SESSION['upload_msg'] = "The file ". basename( $_FILES['file']['name']).
" has been uploaded";


$sql = "UPDATE user SET profilepic='{$picname}' WHERE id = '{$model_user->id}'";
$db=$this->_open_connection();
$result=mysql_query($sql,$db);


header('Location: user.php?view=edit-profile');
}
}
else{
$_SESSION['upload_msg'] = "There was an error uploading the file, please try again!";
header('Location: user.php?view=edit-profile');
}

}


//DISPLAY USER DATA
public function user_data($id)
{
$sql="SELECT * FROM user WHERE id='{$id}'";
$db=$this->_open_connection();
$result=mysql_query($sql,$db);
$row=mysql_fetch_array($result);
$data=array('profilepic'=>$row['profilepic'],
'name'=>$row['name'],
'email'=>$row['email'],
'password'=>$row['password'],
'sex'=>$row['sex'],
'city'=>$row['city'],
'country'=>$row['country'],
'contact'=>$row['contact'],
'aboutme'=>$row['aboutme'],);

return $data;
}

//ALL USER DATA DISPLAY
public function all_user()
{
$sql="SELECT * FROM user";
$db=$this->_open_connection();
$result=mysql_query($sql,$db);
while($row=mysql_fetch_array($result))
{
$data=array('profilepic'=>$row['profilepic'],
'name'=>$row['name'],
'email'=>$row['email'],
'password'=>$row['password'],
'sex'=>$row['sex'],
'city'=>$row['city'],
'country'=>$row['country'],
'contact'=>$row['contact'],
'aboutme'=>$row['aboutme'],);

if( $row['id'] != $_SESSION['uid']){
echo "";
echo $row['username']."

"; }
}
return $data;
}

//ADD COMMENT
public function addComments($model_user)
{
if(!empty($model_user->comment))
{
$sql = "INSERT INTO commnets(comments,my_id,post_id) VALUES('{$model_user->comment}','{$model_user->myid}','{$model_user->postid}' )";
$db=$this->_open_connection();
$result = mysql_query($sql,$db);
return true;;
}
else
return false;
}

//DATABASE CONNECTION
private function _open_connection(){
$con=mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if(!$con)
die('Error Connection:'.mysql_error());
$db_select=mysql_select_db(DB_NAME,$con);
if(!$db_select)
die('Error Selection'.mysql_error());
return $con;
}
}
?>

CONTROLLER
username = $_POST['username'];
$model_user->password = $_POST['password'];

return $model_user->user_registration($model_user);
}

//login
public function user_login()
{
require_once($_SERVER['DOCUMENT_ROOT'].'/mvc2/application/model/user_model.php');

$model_user = new user_model();
$model_user->username = $_POST['username'];
$model_user->password = $_POST['password'];

return $model_user->user_select($model_user);

}

//get userID
public function userID()
{
require_once($_SERVER['DOCUMENT_ROOT'].'/mvc2/application/model/user_model.php');

$model_user = new user_model();
$model_user->username = $_POST['username'];
$model_user->password = $_POST['password'];

return $model_user->userID($model_user);
}

//profile edit
public function profileEdit()
{

require_once($_SERVER['DOCUMENT_ROOT'].'/mvc2/application/model/user_model.php');

$model_user = new user_model();
$model_user->id = $_POST['id'];
$model_user->name = $_POST['name'];
$model_user->email = $_POST['email'];
$model_user->password = $_POST['password'];
$model_user->name = $_POST['name'];
$model_user->sex = $_POST['sex'];
$model_user->city = $_POST['city'];
$model_user->country = $_POST['country'];
$model_user->contact = $_POST['contact'];
$model_user->aboutme = $_POST['aboutme'];

return $model_user->editProfile($model_user);
}

//upload pic
public function uploadProfilePic()
{
require_once($_SERVER['DOCUMENT_ROOT'].'/mvc2/application/model/user_model.php');

$model_user = new user_model();

$model_user->file = $_POST['file'];
$model_user->id = $_POST['id'];

return $model_user->profilepic($model_user);

}

//add comment
public function addComment()
{
require_once($_SERVER['DOCUMENT_ROOT'].'/mvc2/application/model/user_model.php');

$model_user = new user_model();

$model_user->comment = $_POST['comment'];
$model_user->myid = $_POST['myid'];
$model_user->postid = $_SESSION['postid'];


return $model_user->addComments($model_user);
}

//menu
public function menus()
{
echo "My PROFILE ";
echo "REGISTERED USERS ";
echo "LOG OUT ";
}


}



$user = new user();

//view ********************************

if(!empty($_GET['view']))
{
$view = $_GET['view'];
{
switch($view)
{
case 'user-signup':
$page = $user->pageIndex();
break;

case 'user-profile':
$page = $user->profilePage();
break;

case 'edit-profile':
$page = $user->profileViewEdit();
break;

case 'all-user':
$page = $user->AllUserProfile();
break;

case 'visit-profile':
$page = $user->visitProfile();
break;
}
include($_SERVER['DOCUMENT_ROOT'].'/mvc2/application/view/'.$page.'.php');
}
}

//model *******************************************
if(!empty ($_GET[model]))
$model = $_GET['model'];
{
switch($model)
{

//REGISTRATION
case 'user-register':
$result = $user->user_registration();
if($result)
{
$_SESSION['reg_message'] = 'Success register';
}
else
{
$_SESSION['reg_message'] = 'username already exist';
}
$_SESSION['login_message'] = '';
header('Location:user.php?view=user-signup');
break;

//LOG-IN
case 'user-log':
$page = $user->profilePage();
$result = $user->user_login();
if($result)
{
$id = $user->userID();
header('Location:user.php?view=user-profile&id='.$id);
}
else
{
$_SESSION['login_message'] = 'worng username or password, please try again';
header('Location:user.php?view=user-signup');
}
$_SESSION['reg_message'] = '';
break;

//PROFILE EDIT
case 'profile-edit':
$user->profileEdit();

break;

//UPLOAD PICTURE
case 'upload-pic';
$user->uploadProfilePic();
break;
//ADD COMMENT
case 'add-comment';
$user->addComment();
break;
}
}
?>

Register Globals

PHP automatically creates global variables containing data from a variety of external sources EGPCS (Environment, GET, POST, Cookie, Server).register_globals setting in PHP's configuration file (php.ini)(which can be either On or Off) tells whether or not to register the contents of the EGPCS variables as global variables.

Example 1:
To start a session use session_start() and to register a variable in this session use the $_SESSION array.

<?php
session_start();
$_SESSION['my_var'] = 'Hello World';
?>
If register_globals is enabled then your session variables will be available as normal variables on subsequent pages.
<?php
session_start();
echo $my_var;
?>
And If register_globals is enabled, then it will only be in the $_SESSION array.
<?php
session_start();
echo $_SESSION['my_var'];
?>


Example 2:

If register_globals is on, the url http://www.testdomain.com/test.php?id=3 will declare $id as a global variable with no code required.


Note : This feature is a great security risk, and you should ensure that register_globals is Off for all scripts (as of PHP 4.2.0 this is the default).

Assignment #8 Final Project

0) Shopping Cart module is cancelled.
The filenames that are related to the shopping cart are deleted, but their names are recorded in the excel file.

Updated: (To avoid SQL injection attack)
1) adminVal.php --> is updated to avoid "SQL injection attack."

Upload image feature added.
2) item.php (Updated) --> Added a link for "uploading image is added." User must log in as an administrator in order to upload a new image.

3)UploadForm.php --> Accepts ID number of image via get, passes file and ID to uploadImage.php To let user to pick up the image file to upload.

4) UploadImage.php--> To execute the uploading actions. This file is not visible to user. Retrieves image info from uploadForm.php, uploads image over old version of file

Paging Feature is added.
5) Template class: class.recnav.php, class.recnav2.php. are as parent class to extend child classes: RecNav#.php

6)RecNav1.php, RecNav2.php --> extend from class.recnav.php.

7)RecNav3.php --> Extends: class.recnav2.php. RecNav3 uses an updated version of the class, "class.recnav2.php", which allows the developer to pass a category from page to page.

global variable in php

<?php

$num1=10;
$num2=5;

function add()
{
  global $num1,$num2;
  return $num1+$num2;
}

function sub($num1,$num2)
{
  return $num1-$num2;
}

echo 'addition  with out global variables : '. add().'<br>';

echo 'subtraction with global variables : '.sub($num1,$num2);

?>output :

addition with out global variables : 15
subtraction with global variables : 5

Display local country time, instent of displaying server time using SafGetLocalTime

<?
//Display local country time, instent of displaying server time,
//If you country is Sri Lanka and web server in in USA, you can use the following script to solve your problem
//$datetime variable shuld contain the date time like date('Y-m-d H:i:s')
//$diferent variable is the diferent between server and your local country like +5.5 -6
//$mylenth from the final out put you can tell to the function how many character from $myafter sould display
//better to keep $myafter=0 and $mylenth=0 as it is

function SafGetLocalTime($datetime,$myafter=0,$mylenth=0,$diferent){ //get local time pass datetime and diferent
list($year,$month,$day,$hour,$minute,$second) = explode("-",str_replace(array(" ",":"),"-",$datetime));
$hour = ($hour+$diferent);
$datetime = mktime($hour,$minute,$second,$month,$day,$year);

if (
$mylenth==0) {
$datetime = date('Y-m-d H:i:s',$datetime);
}else{
$datetime = date('Y-m-d H:i:s',$datetime);
$datetime = substr($datetime,$myafter,$mylenth);
}
return
$datetime;
}
?>

REST Architecture with PHP

ROA( Resource Oriented Architecture talks about a set of guidelines of implementation of a REST architecture.REST stands for Representational state transfer.REST architectures can be easily implemented in PHP and has wide popularity among the PHP community since it is based on CURD(CREATE, READ, UPDATE, DELETE) model.here are key REST principle mentioned below.

  • Give every “thing” an ID
  • Link things together
  • Use standard methods
  • Resources with multiple representations
  • Communicate statelessly

for more information on REST click here