Thursday, 21 February 2013

str replace for replacing str

<?php
$arr = array("ram","shyam","sita","gita");
print_r(str_replace("shyam","ramesh",$arr,$i));
echo "Replacements: $i";
?>
The output of the code above will be:
Array
(
[0] =>ram
[1] =>ramesh
[2] =>sita
[3] =>gita
)
Replacements: 1

Sunday, 3 February 2013

PHP - Multidimensional Arrays

PHP - Multidimensional Arrays

A multidimensional array is an array containing one or more arrays.
In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.

Example

In this example we create a multidimensional array, with automatically assigned ID keys:
$families = array
  (
  "Sharma"=>array
  (
  "Rahul",
  "Aman",
  "Saurav"
  ),
  "Singh"=>array
  (
  "Manu"
  ),
  "Saxena"=>array
  (
  "Ram",
  "Shyam",
  "Ramesh"
  )
  );
The array above would look like this if written to the output:
Array
(
[Sharma] => Array
  (
  [0] => Rahul
   [1] => Aman
   [2] => Saurav
  )
[Singh] => Array
  (
  [0] => Manu
   )
[Saxena] => Array
  (
  [0] => Ram
   [1] => Shyam
  [2] => Ramesh
  )
)

Tuesday, 29 January 2013

why md5 is used for?and how it is used?

The md5() function uses the RSA Data Security, Inc. MD5 Message-Digest Algorithm.

<?php
$str = "Hello";
echo md5($str);
?>

output
8b1a9953c4611296a827abf8c47804d7

what is Cookie and how it created?

What is a Cookie?

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

How to Create a Cookie?

The setcookie() function is used to set a cookie.
Note: The setcookie() function must appear BEFORE the <html> tag.

Syntax

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

Example 1

In the example below, we will create a cookie named "user" and assign the value "Alex Porter" to it. We also specify that the cookie should expire after one hour:
<?php
setcookie("user", "aman", time()+3600);
?>

<html>
.....
Note: The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received (to prevent URLencoding, use setrawcookie() instead).

Example 2

You can also set the expiration time of the cookie in another way. It may be easier than using seconds.
<?php
$expire=time()+60*60*24*30;
setcookie("user", "Aman Singh", $expire);
?>

<html>
.....
In the example above the expiration time is set to a month (60 sec * 60 min * 24 hours * 30 days).

How to Retrieve a Cookie Value?

The PHP $_COOKIE variable is used to retrieve a cookie value.

In the example below, we retrieve the value of the cookie named "user" and display it on a page:
<?php
// Print a cookie
echo $_COOKIE["user"];

// A way to view all cookies
print_r($_COOKIE);
?>
In the following example we use the isset() function to find out if a cookie has been set:
<html>
<body>

<?php
if (isset($_COOKIE["user"]))
  echo "Welcome " . $_COOKIE["user"] . "!<br>";
else
  echo "Welcome guest!<br>";
?>

</body>
</html>


How to Delete a Cookie?

When deleting a cookie you should assure that the expiration date is in the past.
Delete example:
<?php
// set the expiration date to one hour ago
setcookie("user", "", time()-3600);
?>


What if a Browser Does NOT Support Cookies?

If your application deals with browsers that do not support cookies, you will have to use other methods to pass information from one page to another in your application. One method is to pass the data through forms (forms and user input are described earlier in this tutorial).
The form below passes the user input to "welcome.php" when the user clicks on the "Submit" button:
<html>
<body>

<form action="home.php" method="post">
Name: <input type="text" name="name">
Age: <input type="text" name="age">
<input type="submit">
</form>

</body>
</html>
Retrieve the values in the "welcome.php" file like this:
<html>
<body>

Welcome <?php echo $_POST["name"]; ?>.<br>
You are <?php echo $_POST["age"]; ?> years old.

</body>
</html>

max. upld file size n how v cn chnge d sze

2 mb

we can change the size of the uplaoding file through:

Many shared hosts put very low restrictions on the size of the files that can be uploaded through PHP. But, many hosts also allow you to create your own php.ini file in the root directory of your site. This file can override some of the servers default PHP settings. If not already done simply create a php.ini file and place in the public_html directory of your site. If the server is configured correctly, after inserting this snippet the servers default max upload will be overridden. For this example it was changed to 20 megabytes.
; Maximum file size of post data that PHP will accept.
post_max_size = 20M

; Maximum allowed file size for uploaded files.
upload_max_filesize = 20M
Or you may need to preface with php_value:
php_value upload_max_filesize 100M
php_value post_max_size 100M

submit form without submit button

document.formname.submit()
this is the javascript function from we can submit form without submit button

$_POST, $_REQUEST function




In PHP, the predefined  $_POST variable is used to collect values in a form with method="post".

The $_POST Variable

The predefined $_POST variable is used to collect values from a form sent with method="post".
Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
Note: However, there is an 8 MB max size for the POST method, by default (can be changed by setting the post_max_size in the php.ini file).

Example

<form action="welcome.php" method="post">
Name: <input type="text" name="fname">
Age: <input type="text" name="age">
<input type="submit">
</form>
When the user clicks the "Submit" button, the URL will look like this:
http://www.mieeephp.blogspot.com/welcome.php
The "welcome.php" file can now use the $_POST variable to collect form data (the names of the form fields will automatically be the keys in the $_POST array):
Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo $_POST["age"]; ?> years old.


When to use method="post"?

Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
However, because the variables are not displayed in the URL, it is not possible to bookmark the page.

The PHP $_REQUEST Variable

The predefined $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.
The $_REQUEST variable can be used to collect form data sent with both the GET and POST methods.

Example

Welcome <?php echo $_REQUEST["fname"]; ?>!<br>
You are <?php echo $_REQUEST["age"]; ?> years old.