Thursday, April 5, 2012

Sessions



                 page1                                              page2
          //setting sessions                                  //view
<?php                                                                <?php
session_start();                                               session_start();
$_SESSION['name'] = 'Waqas khan';     echo 'WELLCOME! '.$_SESSION['name'].'</br>Your age is: '
                                                                               .$_SESSION['age']; 
$_SESSION['age'] = 22;                      
?>                                                                 /*if (isset($_SESSION['name'])) {
                                                                       echo 'WELLCOME! '.$_SESSION['name'];
                                                                 } else {
                                                                     echo 'Please log in. ';
                                                                           }
                                                                        */
                                                                               ?>

                 page3
          //unsetting sessions

<?php

session_start();
//unset($_SESSION['name']);
session_destroy();

?>

 *************************************************************                                                                  
                                                               


                                   




                                                                                                                                                                     

Cookies

          page1                                                                        page2

<?php                                                                        
                                                                                         <?php
setcookie('name','waqas khan', time() +10 );                     echo $_COOKIE['name'];
                                                                                       
            //unseting cookie                                                     ?>
                                                                                           
setcookie('name','waqas khan', time() -10 );                            
                                                                                       
?>
*************************************************************************                                                                                    

Thursday, March 22, 2012

Functions


                //Basic founctions
 
 function name() {
     echo 'Waqas</br>';
}
 name();

                 //another example
function table() {
      echo 'Name:</br>Roll #:</br>';
  }
table();

                  //functions with arguments
$n1 =3;
$n2 =4;


function add($numb1, $numb2) {
        echo $numb1 + $numb2.'</br>';
}
add($n1, $n2);

                    //anther example

function ddy($day, $date, $year) {
        echo $day.' '.$date.' '.$year.'</br>';
}
ddy('Monday', 19, 2012);


                      //function with return value
function add2($num1, $num2) {
       $result =$num1 + $num2;
       return $result; 
}

function multiply($num1, $num2) {
       $result = $num1 * $num2;
    return  $result;
}

$sum = multiply(add2(2,2), add2(2,2));
echo $sum .'</br>';

                     //Global Variables and Functions
$ip = $_SERVER['REMOTE_ADDR'];

function ech_ip() {
     global $ip;
    $line = 'Your ip is: '.$ip;
echo $line.'</br>';
}
@ech_ip();


                           //random function
//rand(lower limit , upper limit);

 $rand = rand();
$max_limit = getrandmax();
echo $rand.' / '. $max_limit.'</br>';


               //header function                                            //out put buffer
 //header('Location:'. web addr);             //ob_start(); & ob_end_flush();
$redirect_page = 'https://www.google.com';
$redirect = false;

if ($redirect==true) {
     header('Location: '.$redirect_page);
}


                       //include and require functions & include_once & require_once functions
       page1                                                 page2

<h2>My pap</h2>                            <?php
<?php                                                  require 'hdr.php';
                                                             require_once 'hdr.php';
                                                            ?>
?>

                                                               



                                                                           






String function



                              //String Functions
                        //str_word_count();                  //the 2nd argument z optional

$str = 'my name is waqas.';
$str_word_count = str_word_count($str, 0);
echo 'the string has <b>'.$str_word_count .'</b> words.</br>';

                  //printing in an array
               //str_word_count($str1, 1};
$str1 = 'my name is waqas.';
$an_array = str_word_count($str1, 1);
 print_r($an_array);
echo '</br>';
                  //pirnting in an associative array
               //str_word_count($str2, 2);
$str2 = 'my name is waqas .';
$an_associative_array = str_word_count($str2, 2, '.');
print_r($an_associative_array);

                  //string shuffle
//str_shuffle();
$str3 = 'my name is waqas.';
$shuffle = str_shuffle($str3);
echo $shuffle.'</br>';

                    //sub string
//substr($str4, argument);
$str4= 'my name is waqas';
$sub_string = substr($str4, 4);
  echo $sub_string.'</br>';                

                     //string lenth
                    //strlen();
$str5 ='my name is waqas.';

$str_lenth =strlen($str5);
echo 'string lenth '.$str_lenth.'</br>';

                    //string lenth with for loop
$string_w = 'waqa s';
$string_l = strlen($string_w);
    for ($x=1; $x<=$string_l; $x++) {
     echo $x.' ';
    }

  //A program ussing string shuffle, sub string & string lenth
$str6 = 'my name is waqas';
$shuf = str_shuffle($str6);
$half = substr($shuf, 0, strlen($str6)/4);              //substr($shuf, 0, 10);
echo $half.'</br>';

                   //string revers
  //strrev();
$str7 = 'my name is waqas';
$str_revers = strrev($str7);
echo $str_revers.'</br>';

                   //similar text
   //similar_text();
$str_a = 'my name is waqas..';
$str_b = 'my last name is khan.';
similar_text ($str_a, $str_b, $rsut);
echo 'The similarty is:<b> '.$rsut,' %</b></br>';

                  //String Functions: Upper / Lower Case Conversion
 //strtoupper();, &strtolower();
$upper = 'upper case';
$string_uc = strtoupper($upper);
echo '<b>'.$string_uc.'</b></br>';

$lower = 'LOWER CASE';
$string_lc = strtolower($lower);
echo '<i>'.$string_lc.'</i></br>';

  //string trim
//trim();, ltrim();, &rtrim();

                   //add slashes
  //addslashes(); & stripslashes();
$code = 'my name is<img src="image.jpg"> waqas';
$add_slashes = htmlentities(addslashes($code));
echo stripslashes($add_slashes);


                    //Expression matchion
//preg_match('/word/', $a1);
$a1 = 'find the word';

if(preg_match('/word/', $a1)) {
      echo 'Match found</br>';
} else {
echo 'Match not found</br>';
}

                //preg_match: another program with function
function has_space($a2) {
  if(preg_match('/ /', $a2)) {
    return true;
} else {
return false;
}
}
if (has_space('mynameiswaqas')) {
    echo '<b>line has space</b></br>';
} else {
echo '<b>line has no space</b></br>';
}

                      //string positiion function
 //strpos($b1, $find, $offset);        //offset is optional
$expl = 'my name is waqas';
$loc_pos= strpos($expl, 'is');
 echo $loc_pos.'</br>';
 
                      //string position with while loop
$offset = 0;
$find ='p';
$find_len = strlen($find);

$b1 = 'This is my php file';
  while($find_pos = strpos($b1, $find, $offset)) {
        echo '<b>'.$find.' </b>find at: '.$find_pos.'</br>';
$offset = $find_pos + $find_len;
}
 
                    //String Functions: Replacing Part of a String
//substr_replace($c1, replace, start from, chr leth);
$c1 = 'my name is waqas & my last name is khan';
$string_new = substr_replace($c1, 'and', 17, 1);
echo $string_new.'</br>';
 
                    //String Functions: Replacing Predefined Part of a String
//str_replace(looking for, replace with, string);
$find_w = array('1946', 'bad', 'no');
$replace_w = array('1947', 'good', 'on');

$a2 = 'Pakistan came in to being no 1946 and that is bad';
$str11 = str_replace($find_w, $replace_w, $a2);
echo $str11;

Wednesday, March 21, 2012

Find and Replace Application in php using strlen();, strpos();, & substr_replace(); functions


<?php
$offset = 0;
if(isset($_POST['text'])&& isset($_POST['find'])&& isset($_POST['replace'])) {
 $text = $_POST['text'];
 $find = $_POST['find'];
 $replace = $_POST['replace'];

  $find_len = strlen($find);

       if(!empty($text) && !empty($find) && !empty($replace)) {
 
      while($strpos = strpos($text, $find, $offset)) {
   $offset = $strpos + $find_len;
$text = substr_replace($text, $replace, $strpos, $find_len);
}
        echo $text;
 
  } else {
  echo 'please fill in all fields';
  }
}

?>
<form action="app3.php" method="post">
<textarea name="text" rows="7" cols="30"></textarea></br></br>
Search for:</br>
<input type="text" name="find"></br></br>
Replace with:</br>
<input type="text" name="replace"></br></br>
<input type="submit" value="Find & Replace">
</form>

Word Censoring app in php using str_ireplace(); function


<?php

$find = array('waqas', 'ali', 'wahab');
$replace = array('w***', 'a**', '****');


if(isset($_POST['user_input']) or !empty($_POST['user_name'])) {
 $user_input = $_POST['user_input'];
 $user_input_2 = str_ireplace($find, $replace, $user_input);
 echo $user_input_2;
 }


?>
<hr>
<form action="app2.php" method="post">
<textarea name="user_input" cols="30" rows="6"></textarea></br></br>
<input type="submit" value="submit">
</form>

Php app using strtolwer(); function



<?php

 if (isset($_GET['user_name']) or !empty($_GET['user_name'])) {
   $user_name =$_GET['user_name'];
   $user_name_lc = strtolower($user_name);
   
   if($user_name_lc=='waqas') {
   echo '<strong>Well come!</strong> '.$user_name;
   }
   
}

?>
<form action="app1.php" method="get">
Name: <input type="text" name="user_name"></br>
<input type="submit" value="submib">
</form>