Wednesday, April 8, 2015

Difference between Cookie and Session in PHP

Relation  or Difference between session and cookies in php Cookies and sessions both are used to store values or data,But there are some differences a cookie stores the data in your browser memory and a session is stored on the server.


A cookie is a bit of data stored by the browser and sent to the server with every request.
A session is a collection of data stored on the server and associated with a given user (usually via a cookie containing an id code)

Cookie data is available in your browser up to expiration date and session data available for the browser run, after closing the browser we will lose the session information.

Cookies can only store string data,but session stored any type of data.

we could be save cookie for future reference, but session couldn’t. When users close their browser, they also lost the session.

Cookies are unsecured,but sessions are highly Secured.

You lose all the session data once you close your browser, as every time you re-open your browser, a new session starts for any website.

Cookies stores some information like the username, last visited Web pages etc. So that when the customer visits the same site again, he may have the same environment set for him. You can store almost anything in a browser cookie.when you check the ‘Remember Password’ link on any website, a cookie is set in your browser memory, which exists there in the browser until manually deleted. So, when you visit the same website again, you don’t have to re-login.



Session Basics

Sessions are simply server-side cookies each with a corresponding client side cookie that contains only a reference to its server-side counterpart. When a user visits a page, the client sends the reference code to the server, and PHP will then match that reference code to a server-side cookie and load the data in the server’s cookie into the $_SESSION superglobal.

PROS

  1. Can store very large amounts of data easily.
  2. Save bandwidth by passing only a reference to the session each pageload. A client-side cookie has to pass all of its data.
  3. Data is stored on the web server. This makes sessions secure, because the data cannot be viewed or edited by the client.

CONS

  1. Ends when the browser is closed unless you’ve configured php.ini to extend sessions’ cookie lifetime. Cannot last forever.

Cookie Basics

Cookie data is sent to the web server every page load. PHP reads and stores the value into the $_COOKIE superglobal. When a cookie is created, you can give it a lifespan. After that lifespan runs out, it will expire.

PROS

  1. Can last as long as the website needs. They will still be there even if the browser is closed and reopened.
  2. Useful for “remember me” logins
  3. Useful for storing temporary user settings. For example, if a user is browsing a paginated list of items, sorted a certain way, the sorting setting can be stored in a cookie.

CONS

  1. Stored in the users filesystem. This means that the user can tamper with it and view it.
  2. Can only store a limited amount of data.
  3. Must pass all data to the webserver each pageload. This takes up more bandwidth.

Cookies in Action

CREATING A COOKIE

The function definition:
bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, int secure]]]]])

USING A COOKIE

DELETING A COOKIE

Setting a cookie with no value is the same as deleting it. This will not remove the file from the client computer. To do this, you can set the cookie expiration date to a time in the past, and the browser will take care of it.

Sessions in Action

CREATING A SESSION

This must be called near the top of your code before any output. When you call this function, PHP will check to see if the user sent a session cookie. If so, it will load the session data into $_SESSION. If not, it will create a new session file on the server and send the ID back to the client.

SETTING A VALUE

READING A SESSION VALUE

REMOVING SESSION DATA

ENDING A SESSION





mysql connection from a different domain

I have website example.com, it contains a MySQL server. I have another example.org, both are different domains. I want to access the MySQL server on example.com from example.org. How would it be possible?

Answer
You will need to set the remote example.com when you call the database connection initialization function. For example:
mysqli_connect("example.com", 'username', 'password', 'database name');
But you will need to check whether example.com's MySQL server is set to accept connections from other hosts (see the bind-address directive in your my.cnf or my.ini), and that the username you connect with is set to be able to connect from external domains.
Consider the following Users page of phpMyAdmin: Example user privilege list
It is clear that only user test has access from outside domains (% in the Host field). Not shown on the image, but user test has full privilege on the database called test. The other users are bound to the local domain, even though the server is set to accept connections from the outside, when authenticating, users are thrown a denial.

Friday, April 3, 2015

Mod rewrite PHP to HTML using htaccess.

how you can rewrite all your PHP pages to HTML using a simple .htaccess rule for better search engine indexing. This is a basic category.php to category.html htaccess script.
In this tutorial we are providing a script that can only work if you are hosted on a Linux server.
There is a myth saying that search engines will better index your site if your pages are displayed like static HTML.
The language your site is coded in is irrelevant and also a security vulnerability, if people know what language your site is coded in, they’ll more easily be able to exploit any code vulnerabilities.
Personally I don’t believe it actually makes a difference for search engines if your pages are either PHP or HTML but in the long run it can increase your sites security.
Say for example you have a website and a menu witch is dynamically built using PHP, you could only use it on PHP pages.
Example:
You have menu.php and all the pages of your site: index.php, product.php, services.php, category.php
Using the following script you will be able to access the contents of all your pages using a HTML extension.
The .htaccess file starts here
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [L]
One would also have to change all the links trough out the website from the PHP extension to HTML.
Visitors can now access the contents of products.php using products.html
(336)

Block or allow multiple IP addresses on one line Using htaccess.

Save a little space by blocking multiple IP addresses or ranges on one line. Here are few examples (edit values to suit your needs):
# block two unique IP addresses
deny from 99.88.77.66 11.22.33.44
# block three ranges of IP addresses
deny from 99.88 99.88.77 11.22.33
Likewise, to allow multiple IP addresses or ranges on one line:
# allow two unique IP addresses
allow from 99.88.77.66 11.22.33.44
# allow three ranges of IP addresses
allow from 99.88 99.88.77 11.22.33 (341)

How can we submit a form without using submit buttons?

We can use javascript submit function. We can either use form name or form id to print


What is the difference between explode() and split() functions?

Both are used to split a string to array,
the basic difference is that split() uses pattern for splitting and explode() uses a string.

explode() is faster than split() as it does not match the string based on regular expression. 
 Also split() is deprecated as of 5.3.0. So using of this function is discouraged.