Session Variables Not Working

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
ledtear
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Dec 26, 2019 9:04 am

Thu Dec 26, 2019 9:11 am

Seems like it should be simple, but I have a session variable not showing on a page... I have

Code: Select all

<?php
session_start();
?>
at the top of each page. Where I am creating the variable, I have:

Code: Select all

if ($results==0)
{
	header( 'Location: '.$_SERVER['HTTP_REFERER']."?note=nologin") ;
} else {
	while ($h_row = mysqli_fetch_assoc($hquery)) {
		$_SESSION["entryid"] = $h_row[id];
	}
	header( 'Location: url.php') ;
}
and where I am trying to use it I now am simply trying to

Code: Select all

<? echo "ID: ".$_SESSION["entryid"]?>
but am not getting anything? What should I check?
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 903
Joined: Mon Feb 22, 2016 5:52 pm

Thu Dec 26, 2019 4:07 pm

It looks to me as though you have created an infinite loop?

When you say that
am not getting anything?
Does the page finish loading?

What other code do you have?
ledtear
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Dec 26, 2019 9:04 am

Fri Dec 27, 2019 3:05 am

Yeah, that's just a snippet of the code... the loop runs fine... and on that page the variable does get set... after that code, I can print out the variable and it is fine. The problem is on the next page... the variable does not return anything.
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 903
Joined: Mon Feb 22, 2016 5:52 pm

Fri Dec 27, 2019 8:19 am

We need some more code.
ledtear
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Dec 26, 2019 9:04 am

Fri Dec 27, 2019 8:57 am

ok here's is what I have a stripped down as I can make it... first page is submitted to from a form:

Code: Select all

<?
session_start();
?><?php
$link = mysqli_connect(... ... ...);
if (!$link)
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}				
	if($hquery = mysqli_query($link, "... ... ... query ... ... ..."))
	{
		$results = mysqli_affected_rows($link);
		if ($results==0)
		{
			header( 'Location: '.$_SERVER['HTTP_REFERER']."?note=nologin") ;
		} else {
			while ($h_row = mysqli_fetch_assoc($hquery)) {
				$_SESSION["entryid"] = $h_row[id];
				$_SESSION["entrycode"] = "green";
			}
			header( 'Location: new page...') ;
		}
	}

?>
and then the page I need to use the session variable on...

Code: Select all

<?php
session_start();
?><?php
$link = mysqli_connect(... ... ...);
if (!$link)
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}?><!DOCTYPE HTML>
<html>
	<head>
	</head>
	<body class="homepage">
<?
$uid = 56;
echo "UID: ".$uid."<br/>";
echo "ID: ".$_SESSION["entryid"]."<br>";
echo "Code: ".$_SESSION["entrycode"];
?>	
	</body>
</html>
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 903
Joined: Mon Feb 22, 2016 5:52 pm

Fri Dec 27, 2019 10:47 am

In your first listing, put exit(); after the header(...)

Code: Select all

  header( 'Location: '.$_SERVER['HTTP_REFERER']."?note=nologin") ;
  exit();
} else {
...
  header( 'Location: new page...') ;
  exit();
If this doesn't appear to work, try having this at the top of your second page:

Code: Select all

<?
session_start();
var_dump ($_SESSION);
exit();
If this works, then you know something is wrong in the code after that.

Just want to check that there's fuel in the tank:
I'm taking it for granted that it is the second page that loads.
Do you have error reporting switched on, and display errors on?
Have you checked the error logs?
Are you getting any errors?

Boiler plate:
You should be using PDO in place of MySQLi
ledtear
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Dec 26, 2019 9:04 am

Fri Dec 27, 2019 11:10 am

The page loads fine, just not showing any of the session variables.

Code: Select all

UID: 56
ID:
Code:
I'll have to look into the error reporting...

PDO? Why is that? (I am not familiar with it)
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 903
Joined: Mon Feb 22, 2016 5:52 pm

Fri Dec 27, 2019 12:38 pm

What happened when you ran this:

Code: Select all

<?php
session_start();
var_dump ($_SESSION);
exit();
As the second page?
ledtear
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Dec 26, 2019 9:04 am

Mon Dec 30, 2019 4:28 am

All I got was "NULL"
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 903
Joined: Mon Feb 22, 2016 5:52 pm

Mon Dec 30, 2019 9:04 am

Ok, run the following script and search for session to check that sessions are enabled:

Code: Select all

<?php
phpinfo();
ledtear
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Dec 26, 2019 9:04 am

Mon Dec 30, 2019 9:10 am

I am sure I am doing something wrong but

Code: Select all

1<br/>
<?
phpinfo();
?>
2<br/>
<?
echo phpinfo();
?>
is not returning anything other than:

1
2
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 903
Joined: Mon Feb 22, 2016 5:52 pm

Mon Dec 30, 2019 1:46 pm

Why are you running:

Code: Select all

1<br/>
<?
phpinfo();
?>
2<br/>
<?
echo phpinfo();
?>
instead of:

Code: Select all

<?php
phpinfo();
Does your script filename end with .php ?

Is your file saved in the correct location for the server?

If you view source in your browser, can you see the script in red?
ledtear
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Dec 26, 2019 9:04 am

Thu Jan 02, 2020 4:14 am

So when I do

Code: Select all

<?php
phpinfo();
?>
I get nothing - in the browser or the source... I just added the other stuff to make sure the page was running... yes, it is a .php file... yes, correct location... the file is being executed, just not getting anything returned...
ledtear
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Dec 26, 2019 9:04 am

Thu Jan 02, 2020 6:48 am

This is all I am seeing in the php.ini

Code: Select all

safe_mode=Off
display_errors=Off
upload_max_filesize=2M
post_max_size=8M
max_execution_time=30
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 903
Joined: Mon Feb 22, 2016 5:52 pm

Thu Jan 02, 2020 8:30 am

What makes you say that the file is executing?

Are you typing:
in your browser address bar?

Apart from that, all that I can see is that you either have a problem with the PHP installation, or you are not saving the file in your web server location.
ledtear
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Dec 26, 2019 9:04 am

Thu Jan 02, 2020 9:15 am

Well, in that one example, that's why I put the 1<br>2<br> in it to make sure the file was loading and such... must be some setting issue with my provider... such a pain...
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 903
Joined: Mon Feb 22, 2016 5:52 pm

Thu Jan 02, 2020 9:19 am

I thought you had a local installation. In that case, you need to contact your provider to enable PHP; that's the first time I've heard of it though??
ledtear
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Dec 26, 2019 9:04 am

Thu Jan 02, 2020 9:25 am

Sorry, yes, I am not doing this locally... they provide not php "coding" support and have been very useless in trying to figure out why I cannot get phpinfo.... the crazy thing is that I am really just trying to get sessions work... which work on some pages but not others... I download simple sessions example and they work fine... but not on the pages I need them... ugh...
ledtear
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Dec 26, 2019 9:04 am

Mon Jan 06, 2020 6:13 am

The crazy thing is that these two simple pages that I found online:

Code: Select all

<?php
// page1.php

session_start();
echo 'Welcome to page #1';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();

// Works if session cookie was accepted
echo '<br /><a href="test2.php">page 2</a>';

?>

Code: Select all

<?php
// page2.php

session_start();
echo 'Welcome to page #2<br />';

echo $_SESSION['favcolor']; // green
echo $_SESSION['animal'];   // cat
echo date('Y m d H:i:s', $_SESSION['time']);

// You may want to use SID here, like we did in page1.php
echo '<br /><a href="test1.php">page 1</a>';
?>
work just fine....
ledtear
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Dec 26, 2019 9:04 am

Mon Jan 06, 2020 6:33 am

So here is my full code:

This page is submitted to from a form

Code: Select all

<?php
session_start();
$link = mysqli_connect(xxx,xxx,xxx,xxx);
if (!$link)
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}				
	if($hquery = mysqli_query($link, query))
	{
		$results = mysqli_affected_rows($link);
		if ($results==0)
		{
			header( 'Location: '.$_SERVER['HTTP_REFERER']."?note=nologin");
		} else {
			while ($h_row = mysqli_fetch_assoc($hquery)) {
				$_SESSION["entryid"] = $h_row[id];
				$_SESSION['favcolor'] = 'green';
				$_SESSION['animal']   = 'cat';
			}

			header( 'Location: new location');
		}
	}

?>
and the new location page right now just has

Code: Select all

<?php
session_start();

echo "ID: ".$_SESSION["entryid"]."<br>";
echo "Color: ".$_SESSION['favcolor']."<br>";
echo "Animal: ".$_SESSION['animal']."<br>";
?>	
And yet I get nothing.. HOWEVER... if in page one I do:

Code: Select all

<?php
session_start();
$link = mysqli_connect(xxx,xxx,xxx,xxx);
if (!$link)
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}				
	if($hquery = mysqli_query($link, quesry))
	{
		$results = mysqli_affected_rows($link);
		if ($results==0)
		{
			header( 'Location: '.$_SERVER['HTTP_REFERER']."?note=nologin");
		} else {
			while ($h_row = mysqli_fetch_assoc($hquery)) {
				$_SESSION["entryid"] = $h_row[id];
				$_SESSION['favcolor'] = 'green';
				$_SESSION['animal']   = 'cat';
			}
			echo "ID: ".$_SESSION["entryid"]."<br>";
			echo "Color: ".$_SESSION['favcolor']."<br>";
			echo "Animal: ".$_SESSION['animal']."<br>";
			//header( 'Location: http://www.gradingtheexperts.com/editmymock.php');
		}
	}

?>
When I add:

Code: Select all

echo "ID: ".$_SESSION["entryid"]."<br>";
echo "Color: ".$_SESSION['favcolor']."<br>";
echo "Animal: ".$_SESSION['animal']."<br>";
echo "<a href='http://www.gradingtheexperts.com/editmymock.php'>GO!</a>";
The sessions are still not showing.... HELP!!!
Post Reply