Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Difference between mysql4 and mysql5

Difference between mysql4 and mysql5 

MySQL 5 introduced new important features
1. Cursors
2. Triggers
3. Stored Procedures
4. Views


Bookmark and Share

How to delete file in PHP

How to delete file in PHP:
In this article,I will explain how to delete file using php unlink function with sample example.

unlink: Deletes a file


Bookmark and Share

Difference between mysql_fetch_object and mysql_fetch_array in PHP

difference between mysql_fetch_object and mysql_fetch_array in PHP

mysql_fetch_object :
mysql_fetch_object returns the results from database as objects.(fetches a result row as object)
$result->email


Bookmark and Share

chdir function in php


chdir function in php

The chdir() function changes the current directory to the other directory.

chdir() Syntax:
chdir(string $directory) 

chdir example:
<?php
getcwd();//current directory (e.g.c:\project)
chdir("files");//changes to files directory
echo $current_directory=getcwd();
?> 

Output: C:\project\files


Bookmark and Share

Difference between join and implode in php.


Difference between join and implode in php. 
In this example, I will show how to use join and implode in php.

Join : Join is an Alias of implode().

Example:
<?php
$arr = array('Rajesh', 'India', 'Tutorials');
$str = join(",", $arr);
echo $str; 
?>
Output: Rajesh,India,Tutorials.

implode: implode Returns a string from array elements.

Example:
<?php
$arr = array('Rajesh', 'India', 'Tutorials');
$str = implode(",", $arr);
echo $str; 
?>
Output:Rajesh,India,Tutorials.


Bookmark and Share

Check password length in php


Check password length in php

I have written function to check password length in php

<?php
$password="checkpassword";


$passwordCheck=passwordLength($passwod);


function passwordLength($pwd)
{
    $pwdlen=strlen($pwd);//calculate length of password using strlen function


      if($pwdlen<=4) //if length of password is less than 4 then password is too short.
      {
         return "Password too short!";
      }
    else if ($pwdlen>16) //if length of password is greater than 16 then password is too Long.
   {
         return "Password too Long!";
     }



?>


Bookmark and Share

Regular expression Basics in php

Regular expression Basics in php:
In this article ,i will show how to use regular expressions in php.

 \char escape char

  .            Any single character

 [chars]       One of following chars

 [^chars]      None of following chars

 text1|text2   text1 or text2

 ? 0 or 1 of the preceding text

 * 0 or N of the preceding text  (hungry)

 + 1 or N of the preceding text

 Example: (.+)\.html? matches test.htm and test.html

 (text)  Grouping of text

 Example:  ^(.*)\.html foo.php?bar=$1

  ^    Start of line anchor

  $    End   of line anchor

 Example: ^test(.*) matches test and test1 but not ttest
  (.*)1$ matches a1 and b1, but not test


Bookmark and Share

.htaccess file and URL rewrite rule in php


.htaccess file and URL rewrite rule in php

Redirect URLs using .htaccess  :   
You can use .htaccess to redirect users to a different URL(redirect a url)    
Sometimes you need to redirect some URL and/or page on your site to another one.    
using the “Redirect” directive:    
   
Redirect /folder http://www.example.com/newfolder      
   
Another useful directive is the RedirectMatch    
   
RedirectMatch "\.html$" http://www.example.com/index.php      
   
This will redirect all requests to files that end with .html to the index.php file.      
One of the more powerful tricks of the .htaccess hacker is the ability to rewrite URLs    
   
simple rewriting        
all requests to whatever.htm will be sent to whatever.php:    
Options +FollowSymlinks      
RewriteEngine on      
RewriteRule ^(.*)\.htm$ $1.php [NC]      
RewriteRule ^menu.html menu.php      


Bookmark and Share

Difference between strpos and stripos in php

Difference between strpos and stripos in php


strpos()Function in PHP:


strpos():Returns the position of the first occurrence of a substring in a string.
If the string is not found, this function returns FALSE.

Strpos Syntax:
strpos(string,search,start)

Strpos() function example in php:
<?php
$phpString = 'strpos function in php';
$search   = 'function';
$position = strpos($phpString, $search);


if ($position !== false)
{
     echo "Found at position $position";
}
 else
{
     echo "Not Found";
}
?>

stripos() Function in PHP:


The stripos() function is case-insensitive.
stripos():Returns the position of the first occurrence of a case-insensitive substring in a string.
If the string is not found, this function returns FALSE.

stripos Syntax:
stripos(string,search,start)

stripos () function example in php:
<?php
$phpString = 'strpos function in php';
$search   = 'Function';
$position = stripos($phpString, $search);


if ($position !== false)
{
     echo "Found at position $position";
}
 else
{
     echo "Not Found";
}
?>

Difference between strpos and stripos in php:
  1. stripos works in php5, strpos in php4 and php5.
  2. Unlike the strpos(), stripos() is case-insensitive.



Bookmark and Share

Ob_start() Function -Tutorials and Examples in Php5

Ob_start() function- Tutorials,Examples in Php5


ob_start Function is used to turn on output buffering.The PHP output buffering will save all the server outputs to a string variable.it  returns TRUE on success or FALSE on failure.
If you are using ob_start, then you should also use ob_end_flush  function for flushing the output buffer.

Ob_start() and ob_end_flush()  Example:

<?php
function changeColor($buffer)
{
  return (str_replace("Blue", "Red", $buffer));
}
//ob_start() function in PHP5
ob_start("changeColor");
?>
<html>
<tile>Ob_start() funtion Tutorials,Examples in Php5</title>
<body>
<span>Blue Car</span>
</body>
</html>
<?php
//ob_end_flush() function in PHP5
ob_end_flush();
?>
Output:
<html>
<tile>Ob_start() funtion Tutorials,Examples in Php5</title>
<body>
<span>Red Car</span>
</body>
</html>

Ob_start() and ob_get_clean()  Example:

<?php 
ob_start();
 ?>
<html>
<tile>Ob_start() funtion Tutorials,Examples in Php5</title>
<body>
<span>Blue Car</span>
</body>
</html>
<?php
 $code = ob_get_clean();
?>

Output:
<html>
<tile>Ob_start() funtion Tutorials,Examples in Php5</title>
<body>
<span>Blue Car</span>
</body>
</html>


Bookmark and Share

How To Send Mail with Attachment in PHP

How To Send Mail with Attachment in PHP
I this tutorial,I will explain how to send mail with attachments in PHP.
you can send attachments using a multipart/mixed content header.then convert the attachment into a base64 string.We are  creating a very simple script with php.
the script sends an email to that address with a pdf file(attached file)that I specify in the script.

Here's a simple php example of sending html mail with attached file:

ExampleHow To Send Mail with Attachment in PHP
<?php
$file_path="files/test.pdf";//// Path to the file 
$file_path_type = "application/pdf"; 
$subject = "Testing File attachment"; 

$headers = "From: ".$from. "\r\n" .
"CC: php@example.com";
$from ="test@example.com"; //From email.
$message = "Hello!!<br>";
$to ="rajesh@example.com"; //To email
$file_path_name="test.pdf";

$file = fopen($file_path,'rb');
$data = fread($file,filesize($file_path));
fclose($file);
$semi_rand = md5(time());

$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

//Headers
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

$message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message .= "\n\n";

//convert the attachment into a base64 string
$data = chunk_split(base64_encode($data));

$message .= "--{$mime_boundary}\n" .
"Content-Type: {$file_path_type};\n" .
" name=\"{$file_path_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";

$sent = @mail($to, $subject, $message, $headers);

if($sent) {
echo "Sent Succesfully";

} else {
die("Email not Delivered");
}

?>

I hope this will help you to send email with attachments in PHP.


Bookmark and Share

How to Send Email from a PHP Script using mail() function

How to Send Email from a PHP Script using mail() function.



In this article, I will show you how to send  mail using PHP mail() function in PHP.


PHP mail() function:

The PHP mail() function is used to send emails in php.


Syntax :
mail(string to, string subject, string message, string headers,parameters);


to:  The receiver of the email
subject: The subject of the email.
message: the body of the email.
headers: Additional headers, like From, Cc, and Bcc.


Example:


<?php
$to = "rajesh@example.com";//The receiver of the email
$subject = "Test mail"; //The subject of the email.
$body = "Hello!"; //The body of the email.
$from = "test@example.com";
$headers = "From:" . $from; 
//mail() function
if(mail($to,$subject,$message,$headers)) 
{
   echo("Message successfully sent!");
}
else
{
  echo("Message delivery failed...");
}
?> 


Bookmark and Share

PHP implode() Function


PHP implode() Function


implode : The implode() function returns a string from the elements of an array.


Syntax
implode(separator,array)


Example: implode() 


<?php
$arr = array("PHP","implode","explode" ,"string","functions");
$str = implode(",", $arr);
echo $str; 
?>


OutputPHP, implode, explode, string, functions.


Bookmark and Share

PHP explode() Function


PHP explode() Function


explode :- The explode() function split a string into an array.

Syntax
explode(separator,string)


Note: Separator cannot be an empty string

Example: explode()

<?php


$name= "PHP implode explode string functions";
$arr = (" ", $name);
print_r($arr);
?>


Output:
array([0]=>"PHP",[1]=>"implode",[2]=>"explode",[3]=>"string",[4]=>"functions");


Bookmark and Share

PHP Simple programs


Let us begin with a very simple PHP program. 

Example:

hello.php
<?php
echo "Hello!!.";
?>
Output:Hello!!.


Example Using HTML and PHP
hello.php


<html> 
<head><title>My First PHP Page</title></head> 
<body> 
<span>
<?php 
   $name="Rajesh";
   echo "Hello $name";
?>
</span> 
</body> 
</html> 


Output:Hello Rajesh


Bookmark and Share

Difference between strstr and stristr in php


Difference between strstr and stristr in php.

strstr() and stristr both are used to find the first occurence of the string but stristr( ) is case insensitive.

strstr() - Find first occurrence of a string
Syntax:
strstr ($string, string)  

Example:

<?php
$email = 'rajesh@tEst.com';
$domain_name = strstr($email, 'E');
echo $domain_name; 


?>


Output:Est.com

stristr() - Find first occurrence of a string (Case-insensitive)
Syntax:
stristr ($string, string)  

Example:
<?php
$email = 'rajesh@tEst.com';
$domain_name = stristr($email, 'E');
echo $domain_name; 


?>
Output: esh@tEst.com


Bookmark and Share

Difference between in_array() and array_search() in php?

Difference between in_array() and array_search() in php?
In this tutorial,we will study difference between in_array and array_search.

in_array : -Checks if a value exists in an array
array_search() :-searches an array for a given value and returns the  corresponding key if successful.

in_array Syntax:
in_array(search value, array , strict Boolean);
search value:- will be the value to search in an array
array: will the array to search
strict Boolean: is optional.

Example:

<?php
$name = array("Rajesh", "Sharma", "PHP", "Linux");
if (in_array("Rajesh", $name)) 
{
    echo "Yes";
}
else
{
echo "False";
}
?>

Output: Yes

array_search() :-searches an array for a given value and returns the  corresponding key if successful.

Syntax:
array_search()(search value, array , strict Boolean);
search value:- will be the value to search in an array
array: will the array to search
strict Boolean: is optional.

Example:

<?php
$name = array(0=>"Rajesh", 1=>"Sharma", 2=>"PHP", 3=>"Linux");
echo $key = array_search("Rajesh", $name);

?>
Output: 0


Bookmark and Share

A .htaccess rule that will divert any page in another folder

A .htaccess rule that will divert any page in another folder


1)If you want to redirect http://test.com/f1/  to  http://test.com/f2/.
 .htaccess file will be.

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^f1.*$ http://test.com/f2/ [R=301,L]


2)If you want to redirect http://test.com/f1/test.html to http://test.com/f2/test.html.
 .htaccess file will be.

Options +FollowSymLinks
RewriteEngine On
RewriteRule RewriteRule ^f1/(.*)$ http://test.com/f2/$1 [R=301,L]

3) If you want to redirect http://test.com/ to http://www.test.com/.
 .htaccess file will be.


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test.com [NC]
RewriteRule ^(.*)$ http://www.test.com/$1 [R=301,L]

4) If you want to redirect  http://test.com/ to https://test.com/.

 .htaccess file will be.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.test.com/$1 [R,L]


Bookmark and Share

urlencode and urldecode in php

urlencode :-Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs.

Syntax:
string urlencode ( string $str );

Example:
<?php
$url="Rajesh SharmaTutorials";
$encodeUrl=urlencode($url);
echo '<a href="test.php?sample=$encodeUrl">';
?>

Output: <a href="test.php?sample=Rajesh+Sharma+Tutorials">

urldecode :- Returns the decoded string.(Returns the original string of the input URL).

<?php
$sample=$_GET["sample"];
 echo $sample=urldecode($sample);
?>

Output:Rajesh SharmaTutorials


Bookmark and Share

persistent cookie in php


persistent cookie in php:

A persistent cookie is a cookie which is stored in a cookie file permanently on the browser's computer.
  • Content of a Persistent cookie remains unchanged even when the browser is closed.
  • Persistent cookies are less secure because users can open cookie files see the cookie values.
  • Persistent cookies can be used for tracking long-term information. 
Setting cookie in php:
Cookies in PHP can be set using the setcookie() function

Syntax:
Setcookie(name, value, expire, path, domain);

Example:
<?php
Setcookie(“Full Name”, “Rajesh Sharma”, time()+3600);
?>




Bookmark and Share