Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Broken Session Problem

Participant ,
Oct 14, 2008 Oct 14, 2008
Having learned how to use sessions recently. I thought eveything was going ok.

But for some reason the session is not working or the code has been broken somehow.

I have not edited anything in the section where the session info is.

What my session does is check if logged in = true and displays the visitors username with welcome before the name.

TOPICS
Server side applications
472
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 14, 2008 Oct 14, 2008
The_FedEx_Guy wrote:
> Having learned how to use sessions recently. I thought eveything was going ok.
>
> But for some reason the session is not working or the code has been broken
> somehow.

Don't know where you learned about sessions, but it must have been from
an old book or online tutorial.

> session_register("sessFirstName");
> $sessFirstName = $HTTP_POST_VARS['username'];

Both session_register() and $HTTP_POST_VARS are deprecated, and no
longer work on many servers.

It should be this:

$_SESSION['firstName'] = $_POST['username'];

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 14, 2008 Oct 14, 2008
Hi,
It was on a adobe document online.

I noticed the HTTP_POST_VARS.

But I have the same problem
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 14, 2008 Oct 14, 2008
The_FedEx_Guy wrote:
> Hi,
> It was on a adobe document online.

Well, it's way past its sell-by date.

> But I have the same problem

You have this twice in your script:

<?php session_start();
session_register("sessFirstName");
$sessFirstName = $HTTP_POST_VARS['username'];
?>

You should call session_start() only one in a page.

Other points:

1. You're using this: $_SESSION["sessFirstName"]. The code I gave you
was $_SESSION["firstName"]. It doesn't matter which you use, but they
should match.

2. Does $_POST['username'] exist? It won't exist unless the page is
accessed directly from a form that has been submitted using the post method.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 15, 2008 Oct 15, 2008
$_POST['username'] exists in login.php it works if I take header.php and place it before any other code on login.php, but I get the error below and the username is only displayed.

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\joyrul\login.php:33) in C:\xampp\htdocs\joyrul\includes\header.php on line 4
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 15, 2008 Oct 15, 2008
The_FedEx_Guy wrote:
> $_POST['username'] exists in login.php it works if I take header.php and place
> it before any other code on login.php, but I get the error below and the
> username is only displayed.
>
> Warning: session_start() [function.session-start]: Cannot send session cache
> limiter - headers already sent (output started at
> C:\xampp\htdocs\joyrul\login.php:33) in
> C:\xampp\htdocs\joyrul\includes\header.php on line 4

I have no idea what you mean by header.php, but as I explained before,
you should have only one call to session_start() on a page. Also, you
must make sure there are no newline characters or whitespace outside PHP
tags, or byte-order marks, in either the page itself or include files
before the call to session_start().

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 16, 2008 Oct 16, 2008
I'm sorry. I have included the files below:
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 16, 2008 Oct 16, 2008
The_FedEx_Guy wrote:
> I'm sorry. I have included the files below:

You seem to be doing things in a very roundabout way. Both header.php
and login.php include db.php, and both of them call session_start().
Also, your code to display the welcome message displays the name only if
$_SESSION['logged'] is *not* true.

You don't need header.php at all. You simply need to create a session
variable for the username at the same time as $_SESSION['logged']:

//declare two session variables and assign them
$_SESSION['MM_Username'] = $loginUsername;
$_SESSION['MM_UserGroup'] = $loginStrGroup;
$_SESSION['logged'] = true;
$_SESSION['sessFirstName'] = $_POST['username'];

Then later:

<div class="userWelcome"><?php {
echo 'Welcome '. $_SESSION["sessFirstName"];
} ?></div>

Note that the condition looks like this:

if ($_SESSION['logged'])

You had this:

if ($_SESSION['logged'] == "true")

That means: if $_SESSION['logged'] contains the string, "true". However,
$_SESSION['logged'] doesn't contain a string; it's a Boolean
(true/false) variable.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 16, 2008 Oct 16, 2008
By using header.php I was trying to create a clear website file layout.

So all my include files are in a folder called includes (header.php is in that folder).

It has the top of the website with navigation and anything that needs to be processed first.

On the other pages, DW created the connections to the Database automatically. And if I leave it there I can work with the program and drag/drop the recordset etc.

My Javascripts are in a folder called "js" and CSS files are in "css"

I hope that clears it up for you.

The session works perfectly now. I was not sure where to place the session variable so I just used it in header.php.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 16, 2008 Oct 16, 2008
LATEST
The_FedEx_Guy wrote:
> By using header.php I was trying to create a clear website file layout.
>
> So all my include files are in a folder called includes (header.php is in that
> folder).

Organizing files logically is a very sensible idea.

> The session works perfectly now. I was not sure where to place the session
> variable so I just used it in header.php.

I'm glad that it's working. It sounds as though your problem is not
fully understanding the logical sequence of the code. However, that's
something that will come with experience.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines