How to destroy the php session with one button - php

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)

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?

index.php
<?php session_start(); ?>
<form action="clear-session.php" method="POST">
<input type="submit" value="Clear session" />
</form>
clear-session.php
session_start();
session_destroy();
header('Location: index.php');
exit();

Related

How to delete PHP session by clicking a HTML button?

I want to delete all sessions of my webpage using PHP when the user clicks on a specific button... Below is the related code:
delete sessions
<?php
//when button is clicked then run command: session_destroy()
?>
If it isn't possible then is there a way I can destroy PHP sessions using javascript action listeners?
Hi
One of ways to achive it is simple form and destroy session when the specified variable (a flag) was sent via for example POST.
The HTML form with hidden input and button:
<form action="" method="POST">
<input type="hidden" name="destroySession" value="1">
<input type="submit" value="DESTROY SESSION" />
</form>
PHP code for catch the flag from form:
$destroySessionFlag = filter_input(INPUT_POST, 'destroySession');
if ($destroySessionFlag == 1) {
session_destroy();
}
Cheers

HTML Form Submit but Prevent While Refreshing Page Resubmiting Form

I have a simple form. I am sending the form to same page that has the form. However, after every submit process, then when I want to refresh the page manually, the browser asks me:
Do you want to re-send the form?
How can I prevent this?
foo.php
<?php echo $_POST['id']; ?>
<form action="foo.php" method="post">
<input type="text" name="id" value="">
<input type="submit" value="Send">
</form>
Thanks!
check with a condition if the form was posted, process the form and then redirect using javascript.
<?php
if (!empty($_POST["id"]( {
//do stuff
?>
<script>
location.href=("/");
</script>
<?
}
?>
Method 1: Check with a condition if the form was submited and then redirect to the same page.
if(isset($_POST['button_name_from_form'])) {
#...code
header('Location:page.php');
}
!! But make sure that header is before any output. !!
Method 2: Ajax

Redirect After Submit php [duplicate1]

This is my code:
<input type="submit" name="send" class="do__order" value="order" >
Now, I want to redirect the submitted to any page of my choice after the form data has been submitted, how do I do it?
Add the following line to the script:
header('Location: http://www.example.com/');
You can try something like this on page where you do your check. Let's say it's the check.php
if(isset($_POST['send'])){
header('Location: somewhere.php');
}
And you also need to add a method attribute to your form tag, in this case it would be post, so method="post" and action would be action="check.php".

Form Input to Session Variable

First question I have is that I would like on the index.php to ask the user a question via a form and when the press submit it updates the session variable on jcart.php. With the current code below when I call the session variable later on it is now found so I assume the code I have just now is not working correctly.
The second question is when I press submit it takes me to jcart.php is there a way to avoid this or have it go back.
On my index.php I have a form :
<form action="jcart/jcart.php" method="post">
<input type="text" name="example" id="example" />
<input type="submit" name="submit" value="Submit" />
</form>
And on Jcart.php :
$_SESSION['example'] = $_POST['example'];
Then on the page I am calling it on cocktails.php
<?php
include_once('jcart/jcart.php');
session_start();
?>
<input type="hidden" name="my-item-id" value="<?php echo $_SESSION['example'];?>" />
Thanks for your help.
There is no need to "update the session variable on jcart.php". Once you store a data into the global $_SESSION array, it should be available on all php files, at least until you destroy the session.
That being said, if jcart/jcart.php needs to have a $_SESSION['example'] variable, you need to be sure, that the session is started before including the file, for instance:
<?php
session_start()
include_once('jcart/jcart.php');
?>
For your other question, you can change the action inside your form to whatever you like or issue a header('Location: /'); to redirect to other page after the value was recieved.
Please try this
*jcart.php*
session_start();
$_SESSION['example'] = $_POST['example'];
*then cocktails.php*
include_once('jcart/jcart.php');
echo $_SESSION['example'];
in jcart/jcart.php
session_start();
should be called at the beginning

How do I make a PHP form that submits to self?

How do I make a self-posting/self-submitting form, i.e. a form that submits the results to itself, instead of submitting to another form?
The proper way would be to use $_SERVER["PHP_SELF"] (in conjunction with htmlspecialchars to avoid possible exploits). You can also just skip the action= part empty, which is not W3C valid, but currently works in most (all?) browsers - the default is to submit to self if it's empty.
Here is an example form that takes a name and email, and then displays the values you have entered upon submit:
<?php if (!empty($_POST)): ?>
Welcome, <?php echo htmlspecialchars($_POST["name"]); ?>!<br>
Your email is <?php echo htmlspecialchars($_POST["email"]); ?>.<br>
<?php else: ?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
<input type="submit">
</form>
<?php endif; ?>
I guess , you means $_SERVER['PHP_SELF']. And if so , you really shouldn't use it without sanitizing it first. This leaves you open to XSS attacks.
The if(isset($_POST['submit'])) condition should be above all the HTML output, and should contain a header() function with a redirect to current page again (only now , with some nice notice that "emails has been sent" .. or something ). For that you will have to use $_SESSION or $_COOKIE.
And please. Stop using $_REQUEST. It too poses a security threat.
That will only work if register_globals is on, and it should never be on (unless of course you are defining that variable somewhere else).
Try setting the form's action attribute to ?...
<form method="post" action="?">
...
</form>
You can also set it to be blank (""), but older WebKit versions had a bug.
Try this
<form method="post" id="reg" name="reg" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>"
Works well :)
Your submit button doesn't have a name. Add name="submit" to your submit button.
If you view source on the form in the browser, you'll see how it submits to self - the form's action attribute will contain the name of the current script - therefore when the form submits, it submits to itself. Edit for vanity sake!
change
<input type="submit" value="Submit" />
to
<input type="submit" value="Submit" name='submit'/>
change
<form method="post" action="<?php echo $PHP_SELF;?>">
to
<form method="post" action="">
It will perform the code in if only when it is submitted.
It will always show the form (html code).
what exactly is your question?

Categories