Wednesday, December 24, 2008

php session handling-novice to professional


Introduction:

Sessions are one of the simplest and more powerful tools in a web developers arsenal.
This tool is invaluable in dynamic web page development and it is one of those things
every developer needs to know how to use.

This article explains the basics of PHP Sessions.




Assumptions:


Basic PHP knowledge is required (variables, arrays and such)

HTML Forms.



What are Sessions?

Sessions are a way of storing data. When developing interactive web applications
we often find ourselves in need of a safe place to put certain pieces of information,
such as User ID's and names. Somewhere it won’t be lost every time the browser is
refreshed or redirected. This is exactly what Sessions do. They store your data
on the server, so you can access it at any time, from within any server-side script.

To make this possible a file is created on the server, it is linked to a SessionID
that is generated and sent to the browser as a cookie or through the URL as GET
data.

Then, any time your browser is refreshed/redirected the server-side code reads this
SessionID and loads the information stored in the file on the server.



Why would I use Sessions?

There are endless possible uses for this tool. It is commonly used to keep track
of user information, such as Usernames and UserID's.

For example, if you take a look at the top of the Bytes page your are currently
on. If you are logged in you will see a welcome message and some user controls.
These fields will stay the same no matter where you go on the Bytes web. To make
this possible, your user info must be stored somewhere safe, where the server-side
script will be able to read it. This is the very reason Sessions exists, to make
things like this possible.



How do I use Sessions?

Using Sessions in PHP is very simple. First of all, you need to tell your script
that you are going to be using Sessions.

This is done by invoking the start_session() function. This function will either
create a new session or re-open an existing one. Because this function needs to
send header data to your browser, it must be called before any output is sent.



Once you have told your browser to use
sessions, you can access your session data by calling the $_SESSION super-global.
This is an array, that works pretty much like any other PHP array. You can add,
edit, read and unset it's fields just like you would a normal array.



This is a little example of how to create
a session and use the $_SESSION array:




  1. // Start the session



  2. session_start();






  3. // Create a session variable



  4. $_SESSION['MyVar'] = "This is my session variable";






  5. // Use the session variable



  6. echo "MyVar: ". $_SESSION['MyVar'];






  7. // Edit a session variable



  8. $_SESSION['MyVar'] = "I just edited my first variable";






  9. // Delete a session variable



  10. unset($_SESSION['MyVar']);




Once you have set a field inside the $_SESSION
array, it will be available to any server-side script on the web until the browser
is closed or until the field is manually unset.



A simple example:

Earlier in the article I talked about storing user data with Sessions. This little
example shows how you can gather user information and store it in the PHP Session
array.

It simply asks the user for a username through a HTML form and adds it to the Session.
Then when the user has logged in, it prints a welcome message and gives the user
the option to log out. If the user chooses to log out, it simply unsets the Session
field. effectively logging the user out.




  1. <?php



  2. // Start the session



  3. session_start();




  4. // Check if a username is stored
    in the session



  5. if(isset($_SESSION['Username']))



  6. {



  7. // Check if the user has pressed the logout link



  8. if(isset($_GET['logout']))



  9. {



  10. // Unset the SESSION variable Username



  11. unset($_SESSION['Username']);



  12. echo "You have been logged out <br /> <a href='?'>Continue</a>";



  13. }



  14. else



  15. {



  16. // Print the weclome message



  17. $sUsername = htmlentities($_SESSION['Username']);



  18. echo "Your are signed in as '{$sUsername}'<br /><a href='?logout=true'>Logout</a>";



  19. }



  20. }



  21. else



  22. {



  23. // Check If the user has posted any data



  24. if(isset($_POST['Username']))



  25. {



  26. // Set the SESSION variable Username



  27. $_SESSION['Username'] = $_POST['Username'];



  28. echo "You have been logged in! <br /><a href='?'>Continue</a>";



  29. }



  30. else



  31. {



  32. // Print the login form



  33. echo '



  34. <form action="?" method="post">



  35. Username: <input name="Username" type="text" /><br />



  36. <input type="submit" value="Login" />



  37. </form>';



  38. }



  39. }



  40. ?>

Saturday, December 13, 2008

How Software Gets Done

click here or

Your browser does not support inline frames...However, you can click

here
to see the related document

Send Free SMS Anywhere in India

Click Here to Send Free SMS

Friday, December 5, 2008

How to install and configure PHP 5 on Windows box

It is assumed that you have already successfully setup IIS or installed Apache on your machine and configured it. So, to configure PHP and secure its correct operation you need to go through a couple of steps.



  1. Download and unzip the latest version of PHP

  2. Rename/copy php.ini-recommended to php.ini

  3. Create a session state directory and point the session.save_path variable in php.ini to it

  4. Setup the PHP extensions

  5. Make sure that PHP folder is in the system path

  6. Configure IIS

  7. Configure Apache Web Server

  8. Test your setup

  9. Date Setting

  10. Location of libmysql.dll

Wednesday, December 3, 2008

PHP introduction

What is PHP?



    PHP stands for PHP: Hypertext Preprocessor
    PHP is a server-side scripting language, like ASP
    PHP scripts are executed on the server
    PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
    PHP is an open source software
    PHP is free to download and use
    What is a PHP File?
    PHP files can contain text, HTML tags and scripts
    PHP files are returned to the browser as plain HTML
    PHP files have a file extension of ".php", ".php3", or ".phtml"


What is MySQL?



    MySQL is a database server
    MySQL is ideal for both small and large applications
    MySQL supports standard SQL
    MySQL compiles on a number of platforms
    MySQL is free to download and use
    PHP + MySQL
    PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix platform)


Why PHP?


    PHP runs on different platforms (Windows, Linux, Unix, etc.)
    PHP is compatible with almost all servers used today (Apache, IIS, etc.)
    PHP is FREE to download from the official PHP resource: http://www.php.net/
    PHP is easy to learn and runs efficiently on the server side


Where to Start?


    To get access to a web server with PHP support, you can:
    Install Apache (or IIS) on your own server, install PHP, and MySQL
    Or find a web hosting plan with PHP and MySQL support