Search This Blog

Showing posts with label basics. Show all posts
Showing posts with label basics. Show all posts

imagecreate() or image create in php

<?php
header("Content-Type: image/png");
$img = @imagecreate(110, 20)
  or die("you Can't Initialize new GD image stream");
$background_color = imagecolorallocate($img, 0, 0, 0);
$text_colour = imagecolorallocate($img, 233, 14, 91);
imagestring($img, 1, 5, 5, "hai how are you", $text_colour);
imagepng($img);
imagedestroy($img);
?>



session_destroy() or delete or destroy all sessions in php

<?php

session_start();

session_destroy();


?>

output :

note :
1) no display here
2) but it deletes all sessions

unset or delete or destroy session in php

<?php
session_start();
unset($_SESSION['views']);
?>

output :


note :
1) it deletes "views" session
2) if you already stored   value "views" then it deletes "views" otherwise it raises an error

$_SESSION or session displays in php

<?php
session_start();

echo "Pageviews=". $_SESSION['views'];

?>

output :
 Pageviews=2

note :
1) if already the "views" value store 2 then it displays the value "2"

$_SESSION or sessions in php

<?php
//The session_start() function must put before <html> tag
session_start();

// store session value
$_SESSION['views']=2;

?>

output :

note :
1) no displays here, but stores some value

2) "views" value is now 2

delete cookies in php

<?php

// SET THE expiration time to one hour ago

setcookie("visitor", "", time()-3600);
?>

output :

it deletes "visitor" cookie , here no message displays for security

display cookies in php

<?php

// already user , visitor cookies set

// to Print cookies
echo $_COOKIE["user"].'<br>'.$_COOKIE["visitor"];

?>

output :

here displays already set cookies like "user" , "visitor"

set Cookies in php

<?php
/*

symtax:

setcookie(name, value, expire, path, domain);

*/


//it expires after 5 seconds
setcookie("user", "human", time()+5);
//it expires after 60 sec or 1 min
setcookie("user", "human", time()+60);
//it expires after 3600 sec or 1 hour
setcookie("user", "human", time()+3600);


//expire using varable
//it expires after 10 seconds
$time=10;
setcookie("visitor", "humans", time()+$time);

// note : normally "echo" not use in "setcookies" file
?>

output :


note :

1)no output display here
2)but cookies set

$_POST() in php

<?php

if(isset($_POST['name'])&&isset($_POST['age']))
{
  $name=$_POST['name'];
  $age=$_POST['age'];
  if(!empty($name)&&!empty($age))
  {
   echo '<center>you filled both fields<br><br></center>';
  }
  else
  {
   echo '<center>you not filled both fields<br><br></center>';
  }

}

?>
<center><form action="post.php" method="POST">

               NAME :<input type="text" name="name">

               AGE  :<input type="text" name="age">

                <input type="submit" value="submit">
</form></center>

output :


output :

after filled all fields you clicked on submit button just vies on address bar
it not displays you entered data

$_get() in php

<?php

if(isset($_GET['name'])&&isset($_GET['age']))
{
  $name=$_GET['name'];
  $age=$_GET['age'];
  if(!empty($name)&&!empty($age))
  {
   echo '<center>you filled both fields<br><br></center>';
  }
  else
  {
   echo '<center>you not filled both fields<br><br></center>';
  }

}

?>
<center><form action="get.php" method="get">

               NAME :<input type="text" name="name">

               AGE  :<input type="text" name="age">

                <input type="submit" value="submit">
</form></center>

output :

after filled all fields you clicked on submit button just vies on address bar
it displays you entered data

form with html entities in php

<?php

if(isset($_POST['name'])&&isset($_POST['age']))
{
  $name=$_POST['name'];
  $age=$_POST['age'];
  if(!empty($name)&&!empty($age))
  {
   echo '<center>name :<b>'.$name.'</b> age :<b>' .$age.'</b><br><br></center>';
  }
  else
  {
   echo '<center><b>you not filled both fields</b><br><br></center>';
  }

}

?>
<center><form action="form.php" method="POST">

               NAME :<input type="text" name="name">

               AGE  :<input type="text" name="age">

                <input type="submit" value="submit">
</form></center>

output :

1)if you  filled all fields output displays that is  you entered data

other wise it gives a message as " you not filled both fields "
2) if you give html entities then it also displays as it is

get browser data of visitor in php

<?php
$browser = get_browser(null,true);
print_r($browser);
?>

output :

Array ( [browser_name_regex] => §^.*$§ [browser_name_pattern] => * [browser] => Default Browser [version] => 0 [majorver] => 0 [minorver] => 0 [platform] => unknown [alpha] => [beta] => [win16] => [win32] => [win64] => [frames] => 1 [iframes] => [tables] => 1 [cookies] => [backgroundsounds] => [cdf] => [vbscript] => [javaapplets] => [javascript] => [activexcontrols] => [isbanned] => [ismobiledevice] => [issyndicationreader] => [crawler] => [cssversion] => 0 [supportscss] => [aol] => [aolversion] => 0 )

ckeck ip address is in blocked list or not in php

ip-addr.php :

<?php

include 'ip.inc.php';
foreach($ip_block as $ipb)
 if($ipb==$ip_addr)
 {
   die('<br><br><center><b>your ip is bloacked</center></b>').'<br>';
 }

?>
<center><b><br><br>welcome world</center></b>

=========================================================
ip.inc.php :

<?php

$ip_addr=$_SERVER['REMOTE_ADDR'];
$ip_block=array('222.32.36.25');
?>
===========================================================
run " ip-addr.php "

output :
welcome world

note :
1) if your ip addsess is in block list then is displays  " your ip is bloacked "


header() in php


<?php
header('location:'.'http://http://some-php-programs.blogspot.com/');

?>


output :

automatically http://some-php-programs.blogspot.com/ opened




======================================================


<?php
$webpage='http://some-php-programs.blogspot.com/';
header('location:'.$webpage);

?>

output :
automatically http://some-php-programs.blogspot.com/ opened

$_server() in php

<?php

echo $_SERVER ['SERVER_NAME'].'<br>';
echo $_SERVER ['SERVER_PROTOCOL'].'<br>';
echo $_SERVER ['REQUEST_METHOD'].'<br>';
echo $_SERVER ['HTTP_HOST'];

?>

output :

localhost
HTTP/1.1
GET
localhost



note :

1)some are listed above

modify timestamp in php

<?php

$time=time();

echo 'today date is '.date('d : m : y',$time).'<br>'.'today time is '.date('H:i:s',$time).'<br>';
echo 'today date is '.date('D : M : Y',$time).'<br>'.'today time is '.date('h:i:s',$time).'<br>';

?>

output :
today date is 18 : 07 : 12
today time is 17:35:10
today date is Wed : Jul : 2012
today time is 05:35:10

note :
for every refresh of the browser ( after a second ) the seconds are changing 

rand() in php

<?php

$random=rand();
$maximum_ramdom_number=getrandmax();
echo 'rendom number :'.$random.'<br>';
echo 'maximum rendom number :'.$maximum_ramdom_number.'<br>';

//   or

echo 'rendom number :'. rand().'<br>';
echo 'maximum rendom number :'.getrandmax();

?>

output :
rendom number :30224
maximum rendom number :32767
rendom number :21769
maximum rendom number :32767

note:

1) for every refresh of the browser the random number is changing . but , the maximum random number is not change

timestamp in php

<?php

$time=time();

echo 'today date is '.date('d : m : Y',$time).'<br>'.'today time is '.date('H:i:s',$time);

?>

output :
today date is 18 : 07 : 2012
today time is 17:28:43

preg_match() or search a string / substring in php

<?php

$string='hello world';
if(preg_match("/hel/",$string))
  {
     echo 'word or characted found';
  }
else
  {  echo 'word or characted not found';
  }

?>
output :word or characted found


<?php

$string='hello world';
if(preg_match("/z/",$string))
  {
     echo 'word or characted found';
  }
else
  {  echo 'word or characted not found';
  }

?>

output :word or characted not found

<?php

$string='hello world';
$searchstring='hel';
if(preg_match("/$searchstring/",$string))
  {
     echo 'word or characted found';
  }
else
  {  echo 'word or characted not found';
  }

?>
output :word or characted found
<?php

$string='hello world';
$searchstring='z';
if(preg_match("/$searchstring/",$string))
  {
     echo 'word or characted found';
  }
else
  {  echo 'word or characted not found';
  }

?>

output :word or characted not found
<?php

$string='hello world';
$searchstring='hello';
if(preg_match("/$searchstring/",$string))
  {
     echo 'word or characted found';
  }
else
  {  echo 'word or characted not found';
  }

?>
output : word or characted found

required() in php

index.php :
<?php

@require 'required-page.php';


echo '<br>';
echo 'home page';


?>
output :

database connected

home page
===========================================================
 required-page.php :
<?php

mysql_connect('localhost','root','') or die('not connected');
echo '<center>database connected</center>';

?>
==========================================================
sub-page.php :

<?php

@require 'required-page.php';
echo 'sub page';

?>

output :

database connected
sub page
==========================================================
note :

1) ' @ ' use for don't  display errors related to php

2) if there is no  ' include-page.php ' page then nothing is display in included pages like ' index.php  ' , 'sub-page.php '

3) here ' index.php ' , ' sub-page.php ' pages  are run

Share Links

Related Posts Plugin for WordPress, Blogger...