2

I am doing a small project in that i have user data in session. In the middle the user will do payment, after payment success, the session is destroying automatically.

Now am not able to get user data from session. (How can i achieve this with out using COOKIES).

Note: I have tried using:

header('Access-Control-Allow-Origin: *');    

But no use.

Luthando Ntsekwa
  • 4,192
  • 6
  • 23
  • 52
Ramesh_Konda
  • 111
  • 3
  • 12

2 Answers2

0

Hello this is an example from PHP manual i hope it might help. Firstly start your session by session_start(); and once all your transactions are completed destroy is by session_destroy();

 <?php
    // Initialize the session.
    // If you are using session_name("something"), don't forget it now!
    session_start();

    // Unset all of the session variables.
    $_SESSION = array();

    // If it's desired to kill the session, also delete the session cookie.
    // Note: This will destroy the session, and not just the session data!
    if (ini_get("session.use_cookies")) {
        $params = session_get_cookie_params();
        setcookie(session_name(), '', time() - 42000,
            $params["path"], $params["domain"],
            $params["secure"], $params["httponly"]
        );
    }

    // Finally, destroy the session.
    session_destroy();
    ?>
Rajan
  • 2,427
  • 10
  • 51
  • 111
-1

$_SESSION is a special array used to store information across the page requests a user makes during his visit to your website or web application. While there may be many users accessing the site at the same time, each with his own session, it’s thanks to unique IDs assigned and managed by PHP for each session that allows each user’s session to be available only to himself. Session information is stored on the server rather than the user’s computer (as cookie data is stored), which makes sessions more secure than traditional cookies for passing information between page requests.

Using Sessions

Before you can to store information in a session, you have to start PHP’s session handling. This is done at the beginning of your PHP code, and must be done before any text, HTML, or JavaScript is sent to the browser. To start the session, you call the session_start() function in your first file:

<?php
 // start the session
 session_start();
 // store session data
 $_SESSION["username"] = "Qateel";

session_start() starts the session between the user and the server, and allows values stored in $_SESSION to be accessible in other scripts later on.

In your second file, you call session_start() again which this time continues the session, and you can then retrieve values from $_SESSION.

<?php
 // continue the session
 session_start();
// retrieve session data
echo "Username = " . $_SESSION["username"];

Ending a Session

As important as it is to begin a session, so it is to end one. Even though a session is only a temporary way to store data, it is very important to clean up after yourself to ensure maximum security when dealing with potentially sensitive information. It is also good practice and will avoid having a huge amount of stale session data sitting on the server.

To delete a single session value, you use the unset() function:

<?php
  session_start();
  // delete the username value
  unset($_SESSION["username"]);

To unset all of the session’s values, you can use the session_unset() function:

<?php
 session_start();
 // delete all session values
 session_unset();

Both examples only affect data stored in the session, not the session itself. You can still store other values to $_SESSION after calling them if you so choose. If you wish to completely stop using the session, for example a user logs out, you use the session_destroy() function.

<?php
 session_start();
 // terminate the session
 session_destroy();

Few Tips

Despite there simplicity, there are still ways using sessions can go wrong.

Timing-out sessions is a very important action if you are dealing with users logged in to your website or application.

if (isset($_SESSION["timeout"])) {
   // calculate the session's "time to live"
   $sessionTTL = time() - $_SESSION["timeout"];
   if ($sessionTTL > $inactive) {
      session_destroy();
      header("Location: /logout.php");
   }
}

Use a database to store data at the earliest moment you know the data will be persistent; don’t let it stay as part of the session for too long as this opens it up to possible attack.

Use session_destory() once you don’t need to use the session any more.

You may want to go through:

Community
  • 1
  • 1
Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328