php/html: One auto variable from one php file to another - php

I created forms that automatically generates an ID for a user. I did most of the work with that variable in phpadmin not php. I put NULL for it in php. However, I am creating two pages, first one asks the user to register and then asks him when he is entering a place and leaving. I wish to make it such that when the user registers and is moved to the next page, his ID is also called in that file. This is how I tried to put the variable in the first table:
$_SESSION['idofuser'] = $_GET["id"];
How I put it in the 2nd file that is to call the value of this variable:
$ids=$_SESSION['idofuser'];
Can someone identify the problem?

from PHP documentation, you need to start the session using session_start() or session.auto_start is set to 1 in php.ini to make it accesssible to the next page or until the session die.
see more details in PHP Doc about session.

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

How to use session variables in wordpress

I have a big problem with Wordpress and can't find a solution.
I'm trying to make a value available from every *.php file.
I want with $_SESSION['session_id'] = $var; but I've read that in Wordpress Sessions are not available.
I've tried it but it looks like that my Session is only saved once and after page reload the value is gone.
On my second page I get the Session with $var = $_SESSION['session_id'];
I've tried it with session_start() in my init hook and on my pages too where I use the Sessions but no effect. Is there someone who has an idea who I can transmit my values from one page to another without cookies because I don't want the users to see the values.

PHP Session variable changing from page to page

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();

Need to store a PHP session variable when a link is clicked

In my code I have a link:
<a href='http://www.anotherpageinmywebsite.com'>Click for the other page</a>
When the user clicks this link I need to save a value into a PHP session variable. I don't want to use Ajax, nor an iframe, nor a GET variable (I don't want the session variable appearing to the user as it is sensitive data), EDIT nor do I want to put all the links on the page inside a form.
It seems like it should be possible -- because after all, clicking the link generates a call to the web server to load the new page, and shouldn't I be able to add some code (somehow) to "piggyback" on the natural process a link goes through to post a 'load this new web page' request to the web server?
How can I 'piggyback' on this naturally-occurring sequence of events when the user clicks a link to update a PHP session variable (without using GET variable, ajax or an iframe, preferably).
If we assume that you're using some PHP based framework and you have a controller which serves the pages according to URL - it's quite trivial in fact.
Assuming you are using CodeIgniter - but the logic would be the same for most MVCs out there.
<?php
class Web extends Controller {
// this will fire when you access http://mysite.com/mypage
function mypage($encoded_value) {
$decoded_value = decode($encoded_value);
// although CI has a different way of doing this
$_SESSION['mypagesessionvar'] = $decoded_value;
$this->load->view('mypage'); // this loads the actual page
}
}
I wanted to utilize the default behavior of the browser when a user clicks a link which calls back to the web server -- I wanted to 'piggyback' on that default behavior and send an updated session variable. I was trying to avoid an extra call to the server to update my session variable and I was under the requirement "no GET variables are allowed."
In the end we punted and we coded an Ajax call to set the session variable. We were not able to figure out how to 'piggyback' onto the default behavior of clicking the link without using a GET variable.

Categories