PHP - Session variables not saving from page to page - php

BEFORE YOU MARK THIS AS DUPLICATE, I have read through all the answers on this topic and Non of them worked for me, this is why I am posting this.
So the problem is that the data for $_SESSION is not saving from page to page. Here is my test:
TestOne.php
<?php
session_start();
$_SESSION["user_id"] = 1;
if(isset($_SESSION["user_id"])) {
header("Location: TestTwo.php");
}
?>
TestTwo.php
<?php
if(isset($_SESSION["user_id"])) {
echo $_SESSION["user_id"];
}
?>
It goes to page two but it is a blank page. Why is the data not saving from page to page?
session_save in the php.ini is set to /tmp (I am using hostgator)

You are missing session_start(); on your TestTwo.php
FYI : You need to call session_start(); on all of your PHP files, if you are making use of Sessions.
I have read through all the answers on this topic and Non of them
worked for me, this is why I am posting this.
Really caught my attention btw.

for using session variables, u need to use session_start()
before that
session_start();
if(isset($_SESSION["user_id"])) {
echo $_SESSION["user_id"];
}

You need session_start() on every page that requires the session.

Related

Trying to redirect based on session variable existence but it's not working

I want to ensure that an HTML page only appears if the user has logged in. I'm trying to do it by setting a session variable from the login page then checking if that variable exists when the HTML page is loaded.
This is my code at the very top of the HTML page:-
<?php
session_start();
if (!isset($_SESSION['checks'])) {
header("location: http://localhost/project/fail.php");
}
?>
It doesn't redirect! Nothing happens at all except that the HTML page gets loaded.
Can anyone help please?
Thank you all for your helpful suggestions. The snippet I posted shows the very first lines: i.e. session_start(); is the very first line.
By moving the var check snippet from the session_start() segment and making a separate php check snippet immediately after the body tag, everything works as expected.
You can use header function : https://www.php.net/manual/en/function.header.php
Referring to it :
<?php
session_start();
if (!isset($_SESSION['checks'])) {
header("Location: http://localhost/project/fail.php");
}
?>
make sure that session_start() always come at the first line
if(!isset($_SESSION['checks'])){
header('location: fail.php');
}
I believe your problem is on the login page... Although, if I were to talk about this page, consider trying the following code instead of your snippet first. If it gives the desired outcome then you will know that the problem is with your header and not the session:
<?php
session_start();
if (!isset($_SESSION['checks'])) {
echo "not logged in";
}
?>
Do make sure you're referring to the correct session variable if this code doesn't work and feel free to share how you are starting this session on your login page.

Understanding PHP files and AJAX calls

I'm slowly learning PHP ;-) I'm having difficulties understanding how separate PHP-files work together.
I make AJAX calls to different php files that all need to be connected to the backend (Parse). Such as:
sign_up.php
login.php
verify_email.php
get_something_out_of_the_database.php
What is the standard way to stay logged in over the different php files? (or what is the google search term for it..?)
Update:
Thanks for all your answers about 'sessions'. I doesn't work very well yet, so i made a new question.
Thanks!
Remzo
You should use PHP sessions. These are a way to store information on visitor browser between multiple pages...
To start a session, you first need to add session_start(); in every PHP file you intend to use it. Usually it's added in a header.php
Then, you can use sessions already.
To store a result:
$_SESSION['some_data'] = $var;
To retrieve a result in another page, for example:
echo $_SESSION['some_data']; // will echo $var
More info can be found here:
http://www.w3schools.com/php/php_sessions.asp
You can do this for example by storing the login-data in a session-variable and checking it at the start of every new page.
Example:
You check if login-data is valid. Then
session_start();
$_SESSION["login"] = $loginname;
At the start of another page:
session_start();
if(!isset($_SESSION["login"]) || $_SESSION["login"] != "check_somehow")
{
header("Location: logout.php");
exit;
}
For logging out you can use
session_start();
session_destroy();
On the start of your user logged in, you can do something like
session_start();
$_SESSION['USER'] = <some user info>;
In your other pages you can see if
if(isset($_SESSION['USER'])){
// do something
}
at last on logout
session_destroy();
will kill the session

$_SESSION check if empty and redirect

How to check if $_SESSION['myusername'] is empty and if so redirect them to index.html?
I'm currently using:
<?php
session_start();
/*** begin the session ***/
if(!isset($_SESSION['myusername']))
{
header('Location:index.html');
}
?>
But that lets non-authorized users to stay.
Make use of
<?php
session_start();
/*** begin the session ***/
if(empty($_SESSION['myusername']))
{
header('Location:index.html');;
}
?>
Change your code to this
<?php
session_start();
/*** begin the session ***/
if(!$_SESSION['myusername'])
{
header('Location:index.html');;
}
?>
Maybe you should use a stronger method to control access to your pages, like a RBA system...
Take a look at this, it helped me a lot
Try to like this:
session_start();
if(!isset($_SESSION['blah']) && empty($_SESSION['blah'])) {
header('Location:index.html');
}
exit() or die() after header()
I know this is old; but you did not share the full code.
I know this is from this tutorial here: http://www.phpeasystep.com/phptu/6.html
If you check your logs, you will see that a header cannot be set after HTML tags. If you look at the code from the tutorial, you will notice he has two HTML comments at the top.
Remove those, and PHP will be able to set the header and redirect the user. Otherwise, load the page in the else statement and serve the login form again in the if. Hope this helps.
Also, I am sure you are older and wiser after two years; but this is a poor log in process and (since the code won't work verbatim) a poor tutorial. For example, I can still get the contents of the page with curl http://example.com/protectedpage.php since it will not redirect me, and it will serve the rest of the page.
if(isset($_POST['submit'])){
echo "Success";
}else{
header("location:yoursiteurl");
}

$_SESSION values not holding!

I'm writing a user login system, and I (like so many others) am having a problem with my sessions.
Here's the pointer from the login script when the inputs are validated:
session_start();
$_SESSION['id']=$id;
header('location: memberhome.php');
Here's the first thing on memberhome.php:
<?php
session_start();
$id=$_SESSION['id'];
?>
And later in memberhome.php:
You are logged in as: <?php echo $id; ?>
The problem is $_SESSION['id'] is apparently empty so the echo $id prints nothing.
An alternate that also does NOT work:
//removed session_start and $_SESSION bit from the top
You are logged in as: <?php session_start(); echo $_SESSION['id']; ?>
NOW, here's the weird part. This method DOES work:
You are logged in as: <?php echo session_start();$_SESSION['id']; ?>
You can see the session_start() is moved AFTER the echo. This works when the page loads from the login script. However, upon refresh, it does NOT work once again.
I've tried a bunch of alternatives and spent a few hours searching for answers in previous questions. I also looked at my phpinfo() for something fishy and found nothing. This is entirely what my progress is hinging on. Thanks!
First of all, please enable debugging:
error_reporting(E_ALL);
ini_set('display_errors', '1');
Second, session_start() needs to be at the top of the page. So the line you wrote;
You are logged in as: <?php echo session_start();$_SESSION['id']; ?>
will never work.
The following line needs to be on top of the page, before any HTML etc.
<?php
session_start();
$id=$_SESSION['id'];
?>
Have you tried:
print_r($_SESSION);
to examine the contents of the session?
Make sure you're calling session_start() before you output anything on the page. The standard cookie-based sessions require some header information to be exchanged, which must be done before you send any content.
You're most likely running into output buffering, which is why it sometimes works and other times it does not. Generally speaking, stick to starting the session before any output is generated, you'll find your code works better.
use
ob_start(); #session_start();
on the top of the both page

Restrict access to page with PHP

This seems really simple, and I see a lot of documentation about it, but I can't get it to work. Basically, I have a page "download-software.php" that we want only to be accessed from "download-registration.php" On the second page "download-registration.php" I have this:
<?php
session_start();
$_SESSION['authenticated'] = 'yes';
?>
and on the first page "download-software.php" I have this:
<?php
session_start();
if($_SESSION['authenticated'] != 'yes') {
header("Location: http://kinetick.com/V3/download-free.php");
};
?>
I need to kick the browser to the "download-free.php" page if they dont come from the first page. Can anyone help me out pls?
**Edit**
added session_start(); still doesn't work.
You need to add another session_start() to the beginning of download-software.php to resume the session you started from download-registration.php.
You forgot session_start() on download-software.php
You must always call session_start() before any html data to be able to use $_SESSION in your script

Categories