The GET Method: Query String
The GET Method: Query String
The GET method sends the encoded user information appended to the page request. The page and the encoded
information are separated by the ? character.
http://www.test.com/index.htm?name1=value1&name2=value2
The GET method produces a long string that appears in your server logs, in the browser's Location: box.
The GET method is restricted to send upto 1024 characters only.
Never use GET method if you have password or other sensitive information to be sent to the server.
GET can't be used to send binary data, like images or word documents, to the server.
The data sent by GET method can be accessed using QUERY_STRING environment variable.
The PHP provides $_GET associative array to access all the sent information using GET method.
The POST method does not have any restriction on data size to be sent.
The POST method can be used to send ASCII as well as binary data.
The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By
using Secure HTTP you can make sure that your information is secure.
The PHP provides $_POST associative array to access all the sent information using POST method
Query String
Query strings are a quick, convenient way to pass small amounts of data between browser requests.
Common uses of query strings include remembering a user’s entered keywords when using a search
function, identifying which topic within a forum to display to the user, and specifying which post within a
blog to display.
Query string data is very easy for the user to alter, because it ’ s visible and editable within the browser ’ s
address bar.
Therefore, query strings should be used only in situations where sending incorrect data won ’ t compromise
security.
For example, don ’ t use query strings for storing things such as user IDs (unless your script additionally
verifies that the users are who they say they are).
You also need to make sure you don’t rely on query strings to authenticate users, because people often
send URLs to friends in emails or instant messaging applications.
If your URL contains all the data needed to authenticate a user, and that user sends the URL to a friend,
then the friend can pretend to be them.
When the form data is sent to the server, it is appended to the end of the URL as follows:
In other words, the browser adds a query ( ? ) character to the end of the URL, then follows it with each of
the form fields as “name=value” pairs, with each pair separated by an ampersand ( & ).
The query string is the part of the URL after the ? character.
Creating QueryString:
The great thing about query strings is that they are not limited to form data.
Because a query string is simply a string of characters stored in a URL, you can manually create a URL
containing a query string in your PHP script, then include the URL as a link within the displayed page or in
an email, for example.
PHP even provides some built - in functions to make the process easier.
Here’s a simple example that creates two variables, $firstName and $age , then creates a link in the
displayed page that contains a query string to store the variable values:
$firstName = “John”;
$age = “34”;
$queryString = “firstName=$firstName & amp;age=$age”;
echo ‘ < p > < a href=”moreinfo.php?’ . $queryString . ‘” > Findout more info onthis person < /a > < /p >’;
What is a Session?
A session is a way to store information (in variables) to be used across multiple pages.
A PHP session stores data on the server rather than user's computer.
In a session based environment, every user is identified through a unique number called session identifier
or SID.
This unique session ID is used to link each user with their own information on the server like emails, posts,
etc.
A session is a global variable stored on the server.
Each session is assigned a unique id which is used to retrieve stored values.
Whenever a session is created, a cookie containing the unique session id is stored on the user’s computer
and returned with every request to the server. If the client browser does not support cookies, the unique
php session id is displayed in the URL
Sessions have the capacity to store relatively large data compared to cookies.
The session values are automatically deleted when the browser is closed. If you want to store the values
permanently, then you should store them in the database.
Just like the $_COOKIE array variable, session variables are stored in the $_SESSION array variable. Just
like cookies, the session must be started before any HTML tags.sss
Creating a Session
In order to create a session, you must first call the PHP session_start() function and then store your values
in the $_SESSION array variable.
It returns existing session if session is created already.
If session is not available, it creates and returns new session
The session_start() function first checks to see if a session already exists by looking for the presence of a
session ID.
If it finds one, i.e. if the session is already started, it sets up the session variables and if doesn't, it starts a
new session by creating a new session ID.
We must call the session_start() function at the beginning of the page i.e. before any output generated by
your script in the browser, much like you do while setting the cookies with setcookie() function.
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>
</body>
</html>
To access the session data we set on our previous example from any other page on the same web domain —
simply recreate the session by calling session_start() and then pass the corresponding key to the $_SESSION
associative array.
<?php
// Starting session
session_start();
?>
<?php
?>
Session_destroy removes all the session data including cookies associated with the session.
The stateful and stateless models of software application behavior define how a user’s web browser
communicates with a web server.
In the earliest years of the Web, sites tended to be stateless.
Pages were static, not varying from user to user.
Later, websites included the stateful model, which delivered pages with information unique to each user.
Stateful web applications are essential for modern e-commerce such as online retailers and banks, but
require sophisticated programming to work effectively
Stateless Model
In the pure form of the stateless model, a client program makes a request to an application server, which sends
data back to the client.
The server treats all client connections equally and saves no information from prior requests or sessions.
A website that serves up a simple static web page is a good example of the stateless model.
The server receives requests for pages it hosts and sends the page data to requesting browsers, much like a
short-order cook making meals for diners.
Stateful Model
When an application operates in a stateful mode, the server keeps track of who users are and what they do from
one screen to the next.
Preserving the state of users’ actions is fundamental to having a meaningful, continuous session.
It typically begins with a login with user ID and password, establishing a beginning state to the session.
As a user navigates through the site, the state may change.
The server maintains the state of the user’s information throughout the session until logout.
MySQL implements a database as a directory that contains all files which correspond to tables in the database.
To create a new database in MySQL, you use the CREATE DATABASE statement with the following syntax:
First, you specify the database_name following the CREATE DATABASE clause.
The database name must be unique within the MySQL server instance.
If you try to create a database with a name that already exists, MySQL issues an error.
Second, to avoid an error in case you accidentally c\reate a database that already exists, you can specify the IF NOT
EXISTS option.
In this case, MySQL does not issue an error but terminates the CREATE DATABASE statement instead.
Third, you can specify the character set and collation for the new database at creation time.
If you omit the CHARACTER SET and COLLATE clauses, MySQL uses the default character set and collation for the new
database
1 >mysql -u root -p
2 Enter password: ********
Type the password for the root user and press Enter.
Next, to display the existing database in the server to make sure that you are not creating a new database that
already exists, you use the SHOW DATABASES command as follows:
After that, if you want to review the created database, you can use the SHOW CREATE DATABASE command: