PHP Session variable changing from page to page - php

I'm using a session variable to save the last page the user visited in the search results so that they can go back to that page.
if I make
echo($_SESSION['page'])
at he end of the results pages the value is correct, but when I reload the page (or load any other page) the value is increased by 1. I made a echo($_SESSION['page']) right after the session_start() call and it was already increased, so I guess it's been increased right before the PHP code for the search results finishes running but can't find the line of code that is doing so.
In order to test this added a new variable called $_SESSION['page2'] and this new variable is been increased too.
Any ideas?
EDIT
The variable is taking the value from a Pager object like this
$_SESSION['page'] = $pager->getCurrentPageID();
And I can't find any other place where its been set to a different value. We use the same unit and code in a different site and never had this problem before. Tried replacing the code and the Pager class definition with those from the other site and that didn't fix it.

session_start();
Has to be called before anything is echoed to the browser, best practice is to have it as the first thing you have on your PHP page.
Giving you a better answers is no problem, if you provide more code.
More about session_start();

Related

Where do I end a PHP session so that a variable, created in the first PHP file, can be used in the second file?

I have a website where the user starts on one page and then moves to a second page. The first page initializes and adds values to a counting variable, which I then want to be able to be displayed on the second page. I know to use a session, but I am wondering how I should properly end the session--do I end it on the first page, or the second page? Do both pages need to begin with session_start()?
Yes. Both must use session start. The variable you are talking about is basically termed cookies. The variables will be shared throughout the browser sessions.

PHP session not updating variable

I'm building an image slider where images are loaded from my database and displayed inside a php page.
There's a session variable, "number", to remember what is the current database row, in this way I can easily move to next or previous image.
Initially the page was working perfectly, but now when I press on "Next" button to increase session number and move to next page, it remains stuck to "2" and doesn't allow me to read other pages, even if there are multiple ones. I can only browse image no. 1 and image no. 2
I believe the code is correct, but I'm afraid there's something missing about how PHP sessions works, can you confirm it?
//
session_start() must be the second thing on your page after <?php. HTML tags must be placed after it or the session will not be started.
You do not need to use Session to maintain the pagination, instead Keep the pagination status inside Next, Prev html buttons. I could see some better pagination logic here at:
Simple PHP Pagination script

PHP $_SESSION lost over several pages

I am setting session_start() on every page as very 1st statement.
The 1st page sets $_POST['myVar'] when the page ist submitted. The 2nd page evaluates this by a php lib which sets $_SESSION['myVar'] if this is set in $_POST and not set already in $_SESSION.
When submitting this form and calling the 3rd one evaluating there $_SESSION['myVar'] results that the variable is no more set! There is no session_unset() called between.
Theoretically all is correct but the use shows the opposite. What may there be wrong or still missing?
In the page you have a form, probably your first page session is not required. In the next page(form action page), you need to start the session first. Then you can give like $_SESSION['fieldName']=$_POST['fieldName']. After this in whichever pages you use session_start(), you can have the data. Hope this helps..

Wordpress - SESSION variable does not last

I've spent some time researching answers on this problem, but neither have resulted in fixing my issue. I attempted to make use of the $_SESSION variable. On the first page, I put the result of a input form entry (a name) into it, as it will be used across a variable number of pages. On the page after, the session variable seems to be working fine. I used a var_dump to debug it and everything seems to be set well. Then I click the link I created to go to the next page in this chain of linking. I assume that because the session is being saved, the variable will last until the user exits the website or browser.
Suddenly, on this second page, the session variable becomes null again.
I did a temporary fix where I just pass the GET value from the first page and plug it into the link page links url through some php. It works as intended, but will love to be able to make sessions work when I get to more complicated problems.

Problem with pagination - no success yet

Hey guys I am really messing up with this.
This is I'm doing: If there are 100 users to fetch from Database, I'm using pagination and showing 10 users at a time. when user will click on next page, he will get next 10 users through ajax(called ajax on click) and so on. I'm showing 10 page-links right now with first, next last and previous links.
This is how flow will go: On a.php created links and called ajax function with every link, passing url(b.php) & target(where I will get result), with url also passing clicked pageno., this pageno. will go to b.php and next 10 users will be shown with the help of ajax.
This is the problem: Currently I'm showing 1-10 links with first and last links, unable to show next and previous links because to redirect to next or previous, I am not getting the current page number on a.php i.e i'm passing to b.php. also links are created in foreach loop.
I am trying hard to get this done, but no success yet.
waiting for valuable reply.
Ok, I think I understand...
I think what you need to do is store the current page number as a JavaScript variable. Use the function that is doing the Ajax to update this variable and also update the next/previous links.
Hope this helps.
Update:
On second thoughts, here's a better idea. Having your ajax call directly in your link means you have to put together a url and update it, which is awkward and annoying. This isn't ideal. Instead it is better to store the variables and have some set functions work with them in a set way. So, you could have something like:
<a href="javascript:void(0)" onclick="gotoNextPage()">
Rather than passing them the number of the page to go to, you can store the current page number in a javascript variable and do something like this:
gotoNextPage(){
// lets assume the current page number is stored in a
// variable defined outside of this function called curr_page
curr_page++; // increment the current
call_ajax('user_info.php?pageno='+curr_page);
}
This way the code for the link doesn't ever change.
You can still pass other variables (such as 'order', 'perpage' etc) to gotoNextPage() but you might find you don't need to.
A couple of things to bear in mind:
1. Using jQuery or something similar would probably make things easier for you.
2. Anyone without javascript enabled will not be able to use your site. Consider changing it to something like
<?php
echo '<a href="your_main_page.php?page_number=', ($curr_page + 1), '" onclick="gotoNextPage()">';
?>
This way it would work for via the ajax method but would also work (if not as smoothly and with a page reload) for people without javascript. It's a bit more work for you though...so hat's your choice!
Hope this makes sense. It's been a long day!

Categories