8 Replies - 721 Views - Last Post: 21 February 2015 - 07:50 PM Rate Topic: -----

#1 dak45309   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 155
  • Joined: 22-April 13

PHP session variable passing

Posted 14 February 2015 - 12:50 PM

So...I've ran into a problem and I'm not sure why this is happening. I'm trying to pass a session variable:
$_SESSION["User"]
to my home page (for testing purposes) so I can get the variable to pass. The thing is that it sets these variables at login:
$_SESSION["User"]
$_SESSION["UserType"]
$_SESSION["UserID"]
$_SESSION["Username"]


I know that you have to set session_start() at the top of all the pages you intend to use sessions on so (obviously it's at the top of my login page) and I have it set at the top of my home page (the reason is that it has to be the first thing sent to the browser or it won't work). So here is some of the code I have:

Login page
<?php
session_start();
?>
<!DOCTYPE html>
<head><title>PL Login</title></head>
<body>
<?php
$actionPage = $_SERVER["PHP_SELF"];
echo "<div id='nav'>";
require_once("nav.php");
echo "</div>";
If (isset($_POST["username"]) And isset($_POST["password"]))
{
	require_once("dbconnect.php");
	$db = dbconnect();
	$stmt = $db->prepare("SELECT * FROM Users WHERE Username=:user And Password=:pass");
	$stmt->bindParam(":user", $_POST["username"]);
	$stmt->bindParam(":pass", md5($_POST["password"]));
	If ($stmt->execute())
	{
		$row = $stmt->fetch(PDO::FETCH_ASSOC);
		If ($row != null)
		{
			$_SESSION["User"] = $row["FirstName"] . " " . $row["LastName"];
			$_SESSION["UserType"] = $row["UserType"];
			$_SESSION["UserID"] = $row["UserID"];
			$_SESSION["Username"] = $row["Username"];
			echo "<p>You've logged in successfully!</p>";
			echo "<p>" . $_SESSION["User"] . "</p>";
			$db = null;
		}
		Else
		{
			
			echo "<p>There is no profile with this username!</p>";
			showForm();
		}
	}
	Else
	{
		echo "<p>There is no profile with this username!</p>";
		showForm();
	}
}
Else
{
	showForm();
}
?>
</body>
</html>


In the code above, I echoed the $_SESSION["User"] to see if it had been assigned (and it echoed the user's name meaning it is set at this point).

Home Page Code:
<?php 
session_start();
?>
<!DOCTYPE html>
<head><link rel = "stylesheet" type = "text/css" href="style.css"/><title>Welcome</title></head>
<body>
<div id = "siteHeader">
	<?php $user = $_SESSION["User"]; echo "<p>This is: " . $_SESSION["User"] . "</p>"; ?>
	<div id = "siteName"><h1>Programming Live</h1></div>
	<div id = "login">
	<?php require_once("loginBox.php"); ?>
	</div>
</div>



In the above code (not all but I promise the entire code has ending tags, etc), You can see I try to display the user (as a test), but when I access this page, it just says: "This is: ". Does anyone know what I'm doing wrong cause I don't see it?

Is This A Good Question/Topic? 0
  • +

Replies To: PHP session variable passing

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2667
  • View blog
  • Posts: 10,654
  • Joined: 03-December 12

Re: PHP session variable passing

Posted 14 February 2015 - 12:53 PM

1. You should check to see if a session does exist with that variable before using it.

2 browser tools are your friend. Check to see what is currently in the session area.
Was This Post Helpful? 0
  • +
  • -

#3 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: PHP session variable passing

Posted 14 February 2015 - 12:56 PM

Moved to the PHP section. Please try to post language specific questions in their respective sections, rather than in the generic Web Development section.
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 6,431
  • Joined: 15-January 14

Re: PHP session variable passing

Posted 16 February 2015 - 09:33 AM

You can use print_r to see what the session contains:

print_r($_SESSION);

You should also make sure that error messages are enabled, add this before you use session_start:

ini_set('display_errors', 1);
error_reporting(E_ALL);

Was This Post Helpful? 0
  • +
  • -

#5 dak45309   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 155
  • Joined: 22-April 13

Re: PHP session variable passing

Posted 21 February 2015 - 02:38 PM

On login it shows Array([User] => Dustin Koeller [UserType] => Member [UserID] => 3 [Username] => dak45309), but on my home page, it doesn't have anything set even though the session ids are the same and haven't changed. I have called session_start() on those pages as well as all the other ones that are going to be using it. The only thing I get right now is "User index undefined" because none of the session variables are set on the home page. I'm using a web hosting site to do all of this.

This post has been edited by Dormilich: 24 February 2015 - 06:25 AM

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: PHP session variable passing

Posted 21 February 2015 - 02:58 PM

What/where is the function showForm?
Was This Post Helpful? 0
  • +
  • -

#7 dak45309   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 155
  • Joined: 22-April 13

Re: PHP session variable passing

Posted 21 February 2015 - 03:15 PM

On the login page. I have it do a check with PDO prepared statements. Just a form that allows for login

This post has been edited by Dormilich: 24 February 2015 - 06:25 AM

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: PHP session variable passing

Posted 21 February 2015 - 03:26 PM

And what does that do? Maybe the login is failing but it's reloading the page and you're missing the error message.
Was This Post Helpful? 0
  • +
  • -

#9 dak45309   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 155
  • Joined: 22-April 13

Re: PHP session variable passing

Posted 21 February 2015 - 07:50 PM

The show form is just a function to display the form for the user to log in. When it fails to login, it notifies the user and then redisplays the form via the function. If they login, it shows that they logged in successfully. I know it's not failing because it's all on the same page and it says that I'm logging in successfully (considering I have the $_SESSION variables I'm using at this point in time set. I did a print_r($_SESSION) and it displays that those variables are set. I have a link for them to go back to the home page (which is still supposed to have the sessions working - and it does as I've echoed the session ids on both pages). It's just that the session variables are failing to keep the assigned values to them that I'm trying to assign.

EDIT:

I found out that the $actionPage to load the action of the form is undefined. I tried moving the variable to the first <?php ?> tags and it still came across undefined. This is what it contains:
$actionPage = $_SERVER["PHP_SELF"];


EDIT:

I got that error gone but no luck on the session variables. I have added into the beginning code
ini_set("display_errors", 1);
error_reporting(E_ALL);


No error messages are showing so it's nothing to do with errors.

EDIT:

I decided to test it out on xampp. I was messing around with the site (which can't be found by search engines yet). After testing it on xampp, I found out that the session variables DO pass over. I think it's something to do with the hosting site's settings (possibly). I haven't changed anything so I will contact the hosting site and see if they will enable sessions for my site. :)/> Thanks all! :)/>

This post has been edited by Dormilich: 24 February 2015 - 06:25 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1