Session and Cookies arguably represents something very important in distribution of access rights, among users who are not logged in, the user is logged.
in wordpress for example, only users who are logged in are able to post article, but the user is not logged in can not do it. for example in the forum, you may not create a new thread if you have not logging on, perhaps another example, you can not make a wall status only if you are not logged into your facebook.
All associated with the login, but what is behind the login codes
these, almost all logged on a web application or website that uses session and cookies to differentiate the users who are logged and not logged in, but before knowing more, I'll try to explain what it is first session and what is cookies.
these, almost all logged on a web application or website that uses session and cookies to differentiate the users who are logged and not logged in, but before knowing more, I'll try to explain what it is first session and what is cookies.
Session
Session is a global variable if registered or defined then these variables can be accessed on any page. when we register session, then php will be the browser to save an existing session, so that temporarily stored by the browser session similar to the presupposition of a ticket. every php page that requires a login to check whether the variable session already registered. if it is then, php pages will be allowing users to access. Okay, because the session has a special duty and served as session variable globally that can be accessed on any page, so the session was needed its own function to be able to run it.
The default is to create session like this:
<?php
//function first
session_start();
//and the default is every session_start used
//and when the user accesses these pages usually have
//session_id our own tests
echo session_id ();
//session_id (() will always be different each time you start the browser
?>
//function first
session_start();
//and the default is every session_start used
//and when the user accesses these pages usually have
//session_id our own tests
echo session_id ();
//session_id (() will always be different each time you start the browser
?>
we will make 2 pieces of the file, the first file is used to register / set session, the files are both useful as a page to test whether session working correctly, if true then the second file that can display the contents from the previous session has been registered on the first page of the first file, session_first.php:
<?php
//to create a session, it takes a special function that can
//producing session
//ie, and do not forget to put in row sessio_start
//start after <?php
session_start();
$ _SESSION['first'] = "I was the session";
echo "You has register session'<strong>" .$ _SESSION['first']. "</ strong> '";
session_destroy();
?>
<br/>
<a href="other_page.php"> click here to move the page </ a>
//to create a session, it takes a special function that can
//producing session
//ie, and do not forget to put in row sessio_start
//start after <?php
session_start();
$ _SESSION['first'] = "I was the session";
echo "You has register session'<strong>" .$ _SESSION['first']. "</ strong> '";
session_destroy();
?>
<br/>
<a href="other_page.php"> click here to move the page </ a>
then the next file other_page.php:
<?php
session_start();
echo "You have created a session in this site'<strong> ". $ _SESSION [' first']."</ strong>";
session_destroy();
?>
session_start();
echo "You have created a session in this site'<strong> ". $ _SESSION [' first']."</ strong>";
session_destroy();
?>
We try to test the browser. Likewise if you want to access session has been created on other pages. Suppose we make one any files that this file is also on trial to access. We give the name of the file for example random.php
<?php
//do not forget to include the session_start function of each will
//registering or accessing session
session_start();
//we are trying to access a session variable is set
echo "contents of the current session".$ _SESSION['first'];
?>
//do not forget to include the session_start function of each will
//registering or accessing session
session_start();
//we are trying to access a session variable is set
echo "contents of the current session".$ _SESSION['first'];
?>
Remove or disable Session
There was a question of how to disable or remove session has been created:
There was a question of how to disable or remove session has been created:
<?php
session_start();
unset ($ _SESSION ['first']);
echo "Fill '$ _SESSION [first]' is ...!!! =". $ _SESSION ['first'];
//if you want to destroy all existing session
//you can use session_destroy
//usually this is used when the logout occurred
//all sessions are completely deleted
//usage is like this
session_destroy();
session_start();
unset ($ _SESSION ['first']);
echo "Fill '$ _SESSION [first]' is ...!!! =". $ _SESSION ['first'];
//if you want to destroy all existing session
//you can use session_destroy
//usually this is used when the logout occurred
//all sessions are completely deleted
//usage is like this
session_destroy();
echo "<br /> All sessions have been deleted ..!!!";
?>
?>
Cookie
<?php
//and here we are preparing for the cookie variable
$isicookie = "This is the content of the cookies";
//then we create a cookie with the long time of 1 hour for example ...
//is calculated by using the time unit seconds
setcookie ("cookie1", $ isicookie, time () +3600);
//to be able to access the cookies you can use sintax
echo $ _COOKIE ["cookie1"];
?>
And suppose we want to access the cookie on another page that can also be, we make another page to access the cookies:
<?php
//then directly access the cookie variable
echo $ _COOKIE["cookie1"];
?>
//then directly access the cookie variable
echo $ _COOKIE["cookie1"];
?>
So if you want to delete or remove cookies that exist, we make a fruit
file again deletecookie.php
file again deletecookie.php
<?php
//delete existing cookies with unset
unset($ _COOKIE['cookie1']);
echo $ _COOKIE['cookie1'];
?>
//delete existing cookies with unset
unset($ _COOKIE['cookie1']);
echo $ _COOKIE['cookie1'];
?>
0 komentar:
Posting Komentar