voyeger
voyeger

Reputation: 139

understanding $_POST and $_SESSION variables

What this php code is doing?

$_SESSION['box_status'] = $_POST['box_status'];

is it creating an array or what?, i am lost or it is just making a string value and storing it, or it storing multiple values as append?

According to me, it is just creating a string functionality.

Upvotes: 0

Views: 231

Answers (5)

user4094161
user4094161

Reputation:

@voyeger

As per your code you have post your data (i.e box_status) from php page & you stored your box_status data in to PHP session.(i.e $_SESSION['box_status'] = $_POST['box_status']) .

Basically A session is a way to store information (in variables) to be used across multiple php pages.

Unlike a cookie, the information is not stored on the users computer,it's stored on the server.

So You can use <?php echo $_SESSION['box_status']; ?> to print your box_status data in to any of PHP page.

Don't forget to start the session (i.e session_start();) before print box_status data.

Upvotes: 0

Steven Jeffries
Steven Jeffries

Reputation: 3572

$_POST and $_SESSION are two very special arrays in PHP.

The $_POST array will contain all of the post data sent to it.

<form name='someName' action='thisPage.php' method='POST'>
...
<input type='text' name='someInput'...
...
</form>

Sending this form will bring you to the 'thisPage.php' page. Inside of 'thisPage.php', if you were to access $_POST['someInput'], that would give you the value that was placed inside the textbox named 'someInput'.

PHP sessions are basically a way of storing information about a user on the server while the user continues to browse the page. This is one way of keeping a user logged in while on a site.

Sessions must be started with a session_start() call. Once that happens, PHP will check the client's computer for a session id, and pull up the session variables unique to that id (okay, this is just it in a nutshell, feel free to google for more explicit information if you're curious).

Anyways, by setting $_SESSION['box_status'] = $_POST['box_status'];, you are saying, "Whatever the user sent to me via the POST data in some input called 'box_status', I want to keep track of that value as they continue to browse my site."

I am, of course, generalizing things here, but you should get the point.

Upvotes: 0

Pavel Ondra
Pavel Ondra

Reputation: 38

Depends on what value is sent in POST (e.g. from web form on page).

If there is string in post variable 'box_status' then it saves string into SESSION variable 'box_status'.. if you send array it saves array ..

One important note is that you should serialize/clean POST values before saving them in SESSION

Upvotes: 0

i alarmed alien
i alarmed alien

Reputation: 9520

Your code is setting the value of $_SESSION['box_status'] to the current value of $_POST['box_status'].

$_POST and $_SESSION are reserved variables in PHP; they store the data from a POST operation and session data respectively. They are both associative arrays, which is why they use the $array['key'] format.

Upvotes: 2

KernelCurry
KernelCurry

Reputation: 1297

It is taking the POST variable box_status and assigning it to the session variable box_status

For Reference:

Upvotes: 0

Related Questions