views:

70

answers:

1

I'd like to make a simple form button which completely destroys the session when you click on it. I am just starting to use PHP for the first time, and do not see how I implement it into my HTML code.

What I'd like is simply a form button which will clear the session (and possibly an explanation as to how it works)

+6  A: 

The form button is just like any other form button, nothing special. Catch the POST on the php side of things, and use session_destroy(); to kill the session data entirely.

See this guide for info about forms and post if you're hazy on the subject: http://www.tizag.com/phpT/postget.php and this http://www.tizag.com/phpT/phpsessions.php for info about sessions

More info about forms and PHP and how to work with the data from the form: http://www.tizag.com/phpT/forms.php

Example:

Order.html:

<html><body>
<h4>Tizag Art Supply Order Form</h4>
<form action="process.php" method="post">
<input type="submit" />
</form>
</body></html>

process.php:

<html><body>
<?php
session_destroy();
?>
</body></html>

It's cheesy...does this help?

Caladain
This is correct, but I think the person is asking for you to write the code for them. If not, my mistake. +1 though.
Noctrine
Not a problem..give me a minute :-D
Caladain
That's definitely more than a minute... ;-)
Mike
Not so much write the code as explain where the php gets put (I don't know if I'm making much sense). I saw the short code <?phpsession_start();session_destroy();?>but do not know where it gets put or how to call it. I tried putting it in an external .php file, and using action= to call it, but my browser simply tries to open the php file when I try clicking this.
tom
Sorry, i was having fun trying to figure out how to color format code in SO...relatively new here :-S
Caladain
Ahh, Tom, are you running your code in a PHP enabled webserver? or just trying to open it with Internet Explorer/Firefox?
Caladain
Do take your time, I really appreciate this.
tom
+1 For solution, effort and caring about formatting.
Mike
Good point. You can't test php locally, right?
tom
Right, so you'll need something like XAMPP. http://www.apachefriends.org/en/xampp.htmlThis installs a local copy of apache and everything you need to get going.Store your scripts/programs in the htdocs sub-folder under the XAMPP folder.Then access the script using http://localhost/folder/script.php in your Browser. Be certain that the XAMPP Apache Server is running before calling the script. You can tell via the XAMPP control panel. :-)
Caladain
Thank you so much. You just saved about a day's worth of me trying to figure out why this wasn't working.
tom
Let me know if that doesn't work out for you :-)
Caladain
@tom Make sure you mark Answers as accepted if the solution worked for you.
Noctrine