I have a form and added a session_starts I want to track of how many times I have visited the page ..I want a Button indicating the stop session and the session should stop and start with a new session ...How do I do this?
Suggestion:
Try creating a button in a form and after submitting it change session values to what you want and the start another.
Example
<form method="POST">
<input type="submit" name="emptySession">
<form>
<?php
if(isset($_POST['emptySession']){
//you continue
}
?>
I'd have thought that something like the following ought to work- the logic can be adapted to suit whatever page you have.
<?php
session_start();
if( !empty( $_POST['reset'] ){
#session_unset();
#session_destroy();
#session_start();
#session_regenerate_id( true );
}
?>
<!doctype html>
<html>
<head>
<meta charset='utf-8' />
<title>Sessions</title>
</head>
<body>
<form method='post'>
<input name='reset' value='Reset' type='submit' />
</form>
</body>
</html>
Related
I'm wondering if it would be possible to place two forms on the same page
and saving their cookie values respectively after clicking submit buttons.
With code below, when I click submit button on the first form, its cookie value is saved successfully but when I click on the second form, second value is saved but the first value is overwritten.
<?php
setcookie('username[user111]', $_POST['user111'], time()+60);
setcookie('username[user222]', $_POST['user222'], time()+60);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p>name:<?php echo $_POST['user111']; ?></p>
<form method="POST" action="">
<input type="hidden" name="user111" value="TOM">
<input type="submit" value="close">
</form>
<p>name:<?php echo $_POST['user222']; ?></p>
<form method="POST" action="">
<input type="hidden" name="user222" value="BOB">
<input type="submit" value="close">
</form>
</body>
</html>
I'd like to save both
Name:username[user111] Value:BOB
Name:username[user222] Value:TOM
If I could save the values respectively, it'd not necessary to use form submit
but if possible I'd like to use PHP instead of JavaScript.
Any advice would be appreciated.
I've spent a lot of time today researching this site for my solution but I have had no luck. I'm currently trying to learn php and working on my second project. I can only use PHP. I originally had my delete session and redirect in a separate logout.php file. This was working but then I found out that I can't do this. I've been instructed that I need to "clear the login, delete the session, and redirect back to the login page" and do this within an isPostBack in the results.php file. After a lot of research today I thought I was understanding how to do this but I can't get it to work. Hoping I can get some help.
<?php
session_start();
//require_once('cookies.php');
$isPostBack = filter_input(INPUT_GET, 'submit');
//this is where I need to do the isPostBack for user clicking "logout".
if ($isPostBack) {
// clear ALL session data from memory
// clean up the session and remove the session ID.
// redirect to index.php
endSession();
session_destroy();
header("Location: index.php");
} else {
// user did not click logout doNothing();
}
?>
<html lang="en">
<head>
<title>Results</title>
<link rel="stylesheet" type="text/css" href="">
</head>
<body>
<form action="results.php">
<input type="submit" id="submit" name="submit" value="Logout" />
</form>
<section>
<?php
foreach($_SESSION['answers'] as $answer){
echo "<p>$answer</p>";
}
?>
</section>
</body>
Try to provide name attribute
<input type="submit" id="submit" value="Logout" name="logout"/>
and use only logout variable in place of submit or provide two different fields
$isPostBack = filter_input(INPUT_GET, 'submit');
$isPostBack = filter_input(INPUT_GET, 'logout');
I seem to have found my solution. I needed to give the isPostBack variable a name that matched the name given to the logout button. I also needed to include !==NULL after the isPostBack. I changed endSession(); to $_SESSION = array(); According to my research, endSession(); "removes all session variables". It seems to be working as it should now. Here is my edited code.
<?php
session_start();
$isPostBack = filter_input(INPUT_GET, 'submit')!==NUll;
//this is where I need to do the isPostBack for user clicking "logout".
if ($isPostBack) {
// clear ALL session data from memory
// clean up the session and remove the session ID.
// redirect to index.php
$_SESSION = array();
session_destroy();
header("Location: index.php");
} else {
// user did not click logout doNothing();
}
?>
<html lang="en">
<head>
<title>Results</title>
<link rel="stylesheet" type="text/css" href="">
</head>
<body>
<form action="results.php">
<input type="submit" id="submit" name="submit" value="Logout" />
</form>
<section>
<?php
foreach($_SESSION['answers'] as $answer){
echo "<p> $answer</p>";
}
?>
</section>
</body>
If you need to remove se particular session values you can use unset()
unset ($_SESSION['userid'])
Hi guys I'm trying to build a form wich allow me to create cookies, and I want to be able to delete them as well, to do that there is an option at the bottom of the form that send me to my file call see_cookies.php that generate another form with some php to create a loop that will go through al the $_COOKIE array and print every checkbox with the information. Also some submit buttons with allows my to delete all or the ones I choose, but I'm not able to delete them.
Could anyone help me please.
this is the code for see_cookies.php:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ver::Cookies</title>
</head>
<body>
<h1>Cookies en el sevidor</h1>
<form action="delete_cookies.php" method="post">
<?php
foreach ($_COOKIE as $nombre => $contenido) {
print <<<pinta
<input type="checkbox" name="nombre_cookie[]" value="$nombre"/>$nombre($contenido)<br>
pinta;
}
?>
<input type="submit" value="Delete" name="delete">
<input type="submit" value="Delete_All" name="delete_all">
</form>
</body>
</html>
this is the code for delete_cookie.php
<?php
function delete_cookie($nombre){
setcookie($nombre," ",-10);
}
if(isset($_POST["delete_all"])){
foreach ($_COOKIE as $nombre => $contenido ) {
delete_cookie($nombre);
}
}elseif(isset($_POST["delete"])){
foreach ($_POST["nombre_cookie"] as $key => $contenido) {
delete_cookie($contenido);
}
}
?>
try time less than current time.
setcookie($nombre, "", time()-3600);
Your second file is named delete_cookie.php when the form action goes to delete_cookies.php. I think that's the problem.
I need that both iframes forms work independent (async) but when i submit both forms at the same time the second iframe report "Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\www\iframe2.php on line 2" (when the iframe1 query is a long process) or the second iframe return data AFTER the first process is complete (when the iframe1 query is a short process).
Now, i need keep the session for validate user login.
i need your help, my friends! thanks
index.php
<html>
<head>
<title></title>
</head>
<body>
<iframe src="iframe1.php" width="300" height="400"></iframe>
<iframe src="iframe2.php" width="300" height="400"></iframe>
</body>
</html>
iframe1.php (return query results)
<?php
session_start();
if($_SESSION['user'])
$data="Valid user";
else
header("location: login.php");
set_time_limit(120);
require_once("config.php"); //db conections
if($_POST)
{
//query (long process)
$data.= ""; // concatenated string with query results
}
?>
<html>
<head>
<title></title>
</head>
<body>
<?php echo session_id();?>
<form method="post">
ini:<input type="text" name="var1" value="" /><br />
fin:<input type="text" name="var2" value="" /><br />
<input type="submit" value="Send" />
</form>
Result:<br />
<?php
if(isset($data))
echo session_id()."<hr>".$data;
?>
</body>
</html>
iframe2.php (only return 123456)
<?php
session_start();
if($_SESSION['user'])
$data="Valid user";
else
header("location: login.php");
if($_POST)
{
$data = "123456";
}
?>
<html>
<head>
<title></title>
</head>
<body>
<?php echo session_id();?>
<form method="post">
<input type="text" name="inpt" />
<input type="submit" value="frame2" />
</form>
Result:<br />
<?php
if(isset($data))
echo session_id()."<hr>".$data;
?>
</body>
</html>
Default PHP session have blocking file-access. That means as long as in your first script the session is still active, the second script's access to the session is blocked. PHP will wait until the session is accessible again.
The solution often is to keep the time-span short for the active period. Normally the sessions does not need to be active all the time.
You activate a session with session_start().
You deactivate a session with session_commit().
Locate the part of your script where you actually need an active session. Open it as late as possible (start) and close (commit) it as soon as possible.
maybe very easy!
I'm php coder and I don't have experience in js but I must do this for one of my codes
suppose I have sub1 in page after clicking it must be that sub1 but value now is sub2
<html>
<head>
<title>pharmacy</title>
</head>
<body>
<form method="post" action="pharmacy.php">
<?php
//some code
if(array_key_exists('update',$_POST)){
//somecode
}
?>
<input type="submit" name="update" value="<?php echo if(isset($_GET['update'])) ? 'Show' : 'Update' ?> ">
</form>
</body>
</html>
show as function name does not really make sense here (imo), but you could do:
<input type="submit" name="sub" value="sub1" onclick="show(this)">
and
function show(element) {
element.value = 'sub2';
}
Important:
But that will actually not solve your problem. As soon as you click the button, the form is submitted, meaning the browser initiates a new request and will load a new page. So every change you made the current page is lost anyway.
The question is: What are you trying to do?
It seems to me that you should change the value of the button on the server side. You have to keep track which form was submitted (or how often, I don't know what you are trying to do) and set the value of the button accordingly.
Update:
I see several possibilities to solve this:
You could keep using JavaScript and send and get the data via Ajax. As you have no experience with JavaScript, I would say you have to learn more about JavaScript and Ajax first before you can use it.
You could add a GET parameter in your URL with which you can know which label to show for the button. Example:
<form method="post" action="?update=1">
and
<input type="submit" name="sub" value="<?php echo isset($_GET['update']) ? 'Show' : 'Update' ?> ">
Similar to 2, but use a session variable (and not a GET parameter) to keep track of the state.
Update2:
As you are already having $_POST['update'] you don't need the URL parameter. It could just be:
<html>
<head>
<title>pharmacy</title>
</head>
<body>
<form method="post" action="pharmacy.php">
<input type="submit" name="update" value="<?php echo isset($_POST['update']) ? 'Update' : 'Show'; ?> ">
</form>
</body>
</html>
This should do it
function show(){
document.getElementsByName('sub')[0].value = 'sub2';
return false;
}
Edit: if you don't want it to submit the form, just add a return false, but then you'd need to change your onclick from your submit button to your forms onsubmit;
<html>
<head>
<title>test</title>
<script>
function show()
{
document.getElementById("sub").value= "sub2";
return true;
}
</script>
</head>
<body>
<form method="post">
<input type='submit' id="sub" name='sub' value="sub1" onclick="return show()">
</form>
</body>
</html>