Learning Is Fun

Talks on Web Technology and Better Product Development

Destroy or Delete all session variables in PHP

May2

Sometimes I have seen that coders forges to delete all session variables when the user logs out or sign out. This can be dangerous since this is a security hole. From my own experience I have seen that the session variables may appear even after the sign out. I myself have seen this. This may happen because several people write codes in several ways. But what is important we must delete all session variables when the user signs out.

So, how can we delete all session variables and stop the session variables appear accidentally even after sign out?

Well. We can do it using only 3 lines of code and this is very easy too!

Add these following 3 lines of codes in your sign out code and all your session variables are destroyed!

<?php

$_SESSION = array();
session_destroy();
session_unset();

?>

But if you want to delete the session itself too, you need to add a few lines. So the final code will look like this.

<?php

$_SESSION = array();

if (isset($_COOKIE[session_name()]))
{
setcookie(session_name(), ”, time() – 60000, ‘/’);
}

session_destroy();
session_unset();

?>

Thus, we can destroy our session variables and keep the user secured one way.
OK.
That is all for now.

posted under PHP | 5 Comments »

PHP script to print all the GET & POST variables

March29

Variables are one of the core powers of programming. As PHP programmers, we oftern may find that a $_GET or $_POST variable is not performing properly or the way we expected. This may occur for several reasons such as we made a mistake in the variable name. Sometimes this kind of problem take so much time that lots of time is wasted to correct a single variable!

So, you can use the scripts I have written below and use in your code whenever you suspect that there may be a problem with the variables. These scripts helps you by showing the details of each and every $_GET and $_POST variables in the script.

This is very easy and actually 3 (three) lines of code required.

Below is the code to print all $_GET variables:

<?php

print(‘<pre>’);
print_r($_GET);
print(‘</pre>’);

?>

Click here to see the demo here.

To print all the $_POST variables, we need to change only one line:

<?php

print(‘<pre>’);
print_r($_POST);
print(‘</pre>’);

?>

OK.
You can do it in another way too. It is simple also.

<?php

var_dump($_GET);

?>

Or,

<?php

var_dump($_POST);

?>

So, print your $_GET and $_POST variables whenever you think required.

posted under PHP | 7 Comments »