Very simple problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

hapazoid
Forum Newbie
Posts: 6
Joined: Tue Nov 23, 2010 5:27 pm

Very simple problem

Post by hapazoid »

Hi,

I have a very simple problem that for the life of me can't figure out why it is not working.

I have a Main.html it links to a SellerService.php

This SellerService.php checks whether a user has previously logged in with that user name, if it has it includes a NewBookInfo.html, else it includes a Login.html.

It is a super easy thing to do, but for some reason it is not working in my case.

Code:

<?php
if (IsSet($_SESSION["userName"]))
{
include 'NewBookInfo.html';
}
else
{
include 'Login.html';
}
?>

I have tried all variants of ', ", and none.

Appreciate the help.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Very simple problem

Post by Pazuzu156 »

From what I have learned in php isset shouldn't be IsSet. That could be a problem and might not be. But your code looks pretty much correct to me.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: Very simple problem

Post by s992 »

You're missing session_start(); at the top of the page.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Very simple problem

Post by Pazuzu156 »

s992 wrote:You're missing session_start(); at the top of the page.
That could be your issue. I totally overlooked that. And overlooking that is easy to do.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
hapazoid
Forum Newbie
Posts: 6
Joined: Tue Nov 23, 2010 5:27 pm

Re: Very simple problem

Post by hapazoid »

Thanks for the quick suggestions.

I changed IsSet to isset and I add session_start(); but neither worked.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Very simple problem

Post by Pazuzu156 »

try fixing includes as well. Instead of: include 'my.php'; Use: include("my.php");

See if that helps.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
hapazoid
Forum Newbie
Posts: 6
Joined: Tue Nov 23, 2010 5:27 pm

Re: Very simple problem

Post by hapazoid »

<?php
session_start();
if (IsSet($_SESSION["userName"]))
{
include("NewBookInfo.html");
}
else
{
include("Login.html");
}
?>
It still does not work. All the files are in the same folder.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Very simple problem

Post by Pazuzu156 »

Do you have a method for holding the username? eg. Database, or is it cookie based, or in a predefined variable from the pages you are including?
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
hapazoid
Forum Newbie
Posts: 6
Joined: Tue Nov 23, 2010 5:27 pm

Re: Very simple problem

Post by hapazoid »

I think it is really basic, in that after login, we just assign the username if successful to the _Session["userName"] variable. The login happens after this page though for some reason, not my design.

So this variable has not been set yet, as it will happen once I build out the rest of the pages. Could this be a reason?
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Very simple problem

Post by Pazuzu156 »

You need to have a set username to be able to have a successful login. Your best bet would be to make a page just containing the variables for a fake username and password. Include that with the pages for your session, just to test out the sessions and usernames to see if it works.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
hapazoid
Forum Newbie
Posts: 6
Joined: Tue Nov 23, 2010 5:27 pm

Re: Very simple problem

Post by hapazoid »

That shouldn't matter though. It checks whether the session variable 'userName' has been set or not, if it is null it should go to the else statement.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Very simple problem

Post by Pazuzu156 »

is userName a variable? of so, have userName set: $username = $_SESSION['username']; and in the if: if(isset($_SESSION['username']))
If you already have this I cant help much without seeing the codes from the other pages.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
hapazoid
Forum Newbie
Posts: 6
Joined: Tue Nov 23, 2010 5:27 pm

Re: Very simple problem

Post by hapazoid »

Ok I figured out what the problem was. At the top of the two files that can be included was some html header stuff like doctype and xml version. I guess that stuff wouldnt let it work.

Thanks, for your help.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Very simple problem

Post by Pazuzu156 »

doctype doesnt do much at all, it's just there to say "HEY!" but the html tag has things to set it to xhtml. That could be the reason, although on my website, i have those on my page but my php works.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Very simple problem

Post by McInfo »

hapazoid wrote:At the top of the two files that can be included was some html header stuff like doctype and xml version.
When you include a file, PHP will consider the contents parseable, regardless of the file extension. An installation of PHP that has the short_open_tag option enabled will see the "<?" of the "<?xml" tag in the XHTML file and enter PHP mode. It thinks the first token "xml" might be function, but when the next token is "version" instead of the expected "(", it can't make sense of what it thought was PHP code and dies with a parse error.

One solution is to echo the "<?xml" tag with PHP. Another solution is to disable short_open_tag.
Pazuzu156 wrote:From what I have learned in php isset shouldn't be IsSet.
Language constructs like isset and function names are not case-sensitive. Lowercase is merely a style concern.
Pazuzu156 wrote:Instead of: include 'my.php'; Use: include("my.php");
Again, style.
Pazuzu156 wrote:is userName a variable? of so, have userName set: $username = $_SESSION['username']; and in the if: if(isset($_SESSION['username']))
$_SESSION['userName'] is a variable just like $username would be. I see no need for an alias.
Post Reply