I know there is a tag to read the sessionsmarty.session.. tag but is there tag for destroying the session, like smarty.session.destroy?
What I want is when I click on menu button Data to destroy all stored sessions and start over with clean session.
This is the button that I have
<a href="/data.php" {if $smarty.server.PHP_SELF == "/data.php"}class="selected"{/if}><span>{php}echo __('Data'){/php}</span></a>
I have session destroy in my PHP code where I detect if there is addition parameter in the URL to destroy it and re-load the page with clean session but I don't know if there is such thing for buttons. This is my php
if((isset($_GET['data'])) {
session_destroy();
header("Location: data.php");
exit;
}
So, if user is on https://example.com/data.php?data and refresh the page will load https://example.com/data.php with destroyed sessions.
How can I accomplish the same with the menu button click?
Here is one quick (perhaps ugly) "hack". You can change your button like this:
<a href="/data.php?data" {if $smarty.server.PHP_SELF == "/data.php?data"}class="selected"{/if}><span>{php}echo __('Data'){/php}</span></a>
This way you actually will pass the same parameter with the URL (like the one you expect in the if condition) and when you click on the button it will be cleaned and redirected to /data.php from the if condition. You will get one extra redirect on the button click.
Related
I'm trying to build a website with only 2 pages.
Page 1
In this page theres a buttom and everytime the buttom is clicked it increases the value of the label on page 2.
Page 2
In this page there is only one label a value that is increased by page one
Could someone give me an example
consider the following link as a solution to your problem using AJAX and jQuery.
http://tutorialzine.com/2009/09/simple-ajax-website-jquery/
Go through sessions
In your page one
<?php
session_start();
$_SESSION['myValue']=10; // here your value.
?>
In page two
<?php
session_start();
echo $_SESSION['myValue'];
?>
Another way
<a href='pageone.php?myValue=12'>click</a>
then in any other page, you can access
$_GET['myValue'];
Depending on what resources you have available, you could use AJAX to save the value to the database/XML file on PAGE 1. Then write a script to retrieve the value on PAGE 2.
Alternatively, you could look into saving a SESSION variable on PAGE 1 and retrieve that on PAGE 2.
Session Links:
PHP SESSION Tutorial
PHP Manual - SESSIONS
Js for the button page
var counter=0;
function buttonclick(){
counter++;
window.postMessage(counter);
}
Js for display:
window.onload=function(){
window.addEventlistener("message", handle,false);
}
function handle(event){
alert(event.data);
}
This uses the browsers internal messaging system to transfer the value from one window to another. If you want to transfer it even if page2 is not opened use AJAX to send it to the server and use SESSIONS to store it.
The home page on my website has a form with two fields: Make of the Car (carmake) and Car Model (carmodel). Upon submit, it takes the user to the 2nd page that displays information about the specific make and model that the user has input on the homepage.
I am using post-method to pass the variables to the 2nd page, where I start a new session with the two variables:
<?php
session_start();
$_SESSION['carmake']=$_POST['carmake'];
$_SESSION['carmodel']=$_POST['carmodel'];
?>
This works fine. On this 2nd page, I have a link for a 3rd page that uses the same two session variables. So on the 3rd page, I start the page with the session function and the session variables are available for use. No problem so far.
There are 2 problems I am experiencing from this point onwards:
Problem 1: I have a button on the 3rd page that links back to the 2nd page. When I use this button to go back to the 2rd page, the 2nd page does not echo the session variables. However, if I use the browser 'back' button to go back to the 2nd page, I am asked to "confirm resubmission" which I do and then 2nd page correctly echoes the session variables.
My question 1: How can I design the navigation to previously visited pages without losing the session variables?
Question 2: Is there a way to avoid having to "confirm resubmission" (when using the browser back button) on a previously visited page that uses session variables?
(I am a newbie here and apologize in advance for the long post for what may be a very simple question.)
Question 1 solution:
Change your code
<?php
session_start();
$_SESSION['carmake']=$_POST['carmake'];
$_SESSION['carmodel']=$_POST['carmodel'];
To
<?php
session_start();
if(isset($_POST['carmake']))
$_SESSION['carmake']=$_POST['carmake'];
if(isset($_POST['carmodel']))
$_SESSION['carmodel']=$_POST['carmodel'];
It will set session value only if form is submit (which is submit from Form1), so when you click back button, it will not set blank values and will keep last session value remains)
Question 2 solution:
<?php
session_start();
if(isset($_POST['carmake']))
$_SESSION['carmake']=$_POST['carmake'];
if(isset($_POST['carmodel']))
$_SESSION['carmodel']=$_POST['carmodel'];
if(isset($_POST['carmake']) || isset($_POST['carmodel'])){
# Redirect on same page when submitting the form
# will not ask for form submission when click back on browser page
header("Location: page2.php")
exit;
}
Problem 1: Unless you are checking to see if $_POST has values before you assign them, the sessions will get empty values. Turn on a higher error log level and display errors and you should see a notice about an undefined variable. Also, it works when you confirm submission because you are re-POSTing the data to the second page.
Question 1: Check that the POST variables are not empty() before assigning them.
Question 2: Not that I am aware of, you are going back to a page that your browser previously POSTed to and it wants to know if you want to again (to get the "same" results as before)
UPDATE: Also, you should post more code.
I know I am late here, but just thought to share my solution. I hope this helps someone.
Question 1 : Solution
Store the value in session only if form is submitted.
<?php
session_start();
if(isset($_POST['carmake'])) $_SESSION['carmake'] = $_POST['carmake'];
if(isset($_POST['carmodel'])) $_SESSION['carmodel'] = $_POST['carmodel'];
/* Rest of the code goes here */
Question 2 : Solution (easy alternative)
Cache control and Session cache limiter prevents form resubmission when using browser back button.
<?php
session_start();
header("Cache-Control: no cache");
session_cache_limiter("private_no_expire");
/* Rest of the code goes here */
~ Full code ~
<?php
session_start();
header("Cache-Control: no cache");
session_cache_limiter("private_no_expire");
if(isset($_POST['carmake'])) $_SESSION['carmake']=$_POST['carmake'];
if(isset($_POST['carmodel'])) $_SESSION['carmodel']=$_POST['carmodel'];
/* Rest of the code goes here */
I have listed projects in main work page, If i click on the project it will direct to work page where details have been displayed,If i click on the back button its is redirecting main work page.I want back button should redirect the previous project please help.
For your back button to redirect you to the previous project, you need to change the present HTML from
BACK
to :
BACK
You will have to do this dynamically by calculating the page numbers in the href as $_GET['page'] - 1
<? $page = (int) $_GET['page']; $back = $_GET['page'] - 1;
print 'BACK';
?>
The back button in my browser is working as-designed on your site: it takes me to the previous webpage I was on.
Websites cannot (easily) alter the behaviour of the browser back button in order to keep the user in-control. You can't (easily) use the back button to navigate through a website (if I understand your question correctly).
why not change the back button client-side?
<a href='#' id='backBtn'>Back</a>
And in JS:
document.getElementById('backBtn').onclick = function()
{
history.back();
};
If all you want is for the back btn to behave like the browser back button?
i have simple coding problem. i have created a page with textbox and share button. the page also contains one Points up button.
i had a problem with that points up button that when the user click on that button and refresh the page ... a window ask for resend of information
for that i have used following code which works fine.
`header('Location: samepageurl.php');
exit;`
but the problem with above code is when user scroll down page and click the button. the page automatically scrolls up. and user have to manually scroll it down.
what i want is the page should refresh but it should be on the same location where it was.
if the problem is still unclear please refer the following images
You can set a fragment identifier.
eg:
<a name="points_up"></a> <!-- this needs to be near that button, the page will scroll exactly where the element is -->
and redirect him to:
header('Location: samepageurl.php#points_up');
die;
Mihai answer is correct, but as you said that fragment identifier is not working because each user has points up button, you can pass user id as a fragment identifier and make a hidden(display : none;) <a> tag and pass the user id in front of each user...
Like this:
You can set a prefix before a user id too (optional)
<a name="pu12345" style="display: none;"></a>
<?php
header('Location: whatever.php#pu12345');
exit;
?>
You can send the request via ajax instead relying on the normal form submission. That will not affect the scrolling of the current page.
Add this line at the bottom of your page before the the <\body> tag
<button id="PageRefresh">Refresh a Page in jQuery</button>
<script type="text/javascript">
$('#PageRefresh').click(function() {
location.reload();
});
</script>
I have one question which is somewhat two-parted (though the parts go hand in hand). I've started picking up PHP, and I wanted to do two things when an image is clicked.
I want the click to
Increment a session variables, say $_SESSION['entry'].
Reload the current page (say index.php).
How should I go about this?
To be clear, I'm not asking for someone to code this for me, I'd just like to be pointed in the right direction because I'm not too sure what the best way would be.
Well, anchor links "reload" the page if the href points to the same page. So, all you need to do is tell PHP you want to increment the session variable. You could use a GET variable to do this:
Increment the counter
And then in your index.php:
if (isset($_GET['increment']) && $_GET['increment'] == 'true') {
$_SESSION['counter']++;
}
This assumes you've already initialized the session variable counter at some point. You can check out the wonderful PHP docs to explain the functions used above if you aren't familiar with them.
The way to do this would be to link the image to "itself" $_SERVER['PHP_SELF'] perhaps or just to /index.php, and check the session to see if that value is set, and if so increment it.
<?php
session_start();
if (isset($_SESSION['entry'])) {
$_SESSION['entry']++;
} else {
$_SESSION['entry'] = 1;
}
// if entry is greater than some value in your DB, then set it back to 1
<img src=.../>
<?php if($_GET['incr']) $_SESSION['entry']++; ?>
this should give you the idea.
You could do an AJAX call to a PHP script that increments $_SESSION['entry'].
Load page with image that has a link around it: "?imageClick=1" for instance
On image click the page is therefor automatically loaded
If $_GET[ 'imageClick' ] equals 1 increment the session variable
Redirect to same page without the imageClick variable
If you are concerned that index.php?imageClick=1 may be remembered by the browser in it's history, and therefor can be used to reload without an actual image click:
Load page with a form that has method POST and an input element of type image, named imageClick (acting as a submit button) with value 1
On image button click the form is submitted to the same page
If $_POST[ 'imageClick_x' ] or `$_POST[ 'imageClick_y' ] is set and increment the session variable
Redirect to same page