Page 1 of 2

Very simple problem

Posted: Tue Nov 23, 2010 5:31 pm
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.

Re: Very simple problem

Posted: Tue Nov 23, 2010 5:40 pm
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.

Re: Very simple problem

Posted: Tue Nov 23, 2010 5:46 pm
by s992
You're missing session_start(); at the top of the page.

Re: Very simple problem

Posted: Tue Nov 23, 2010 5:50 pm
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.

Re: Very simple problem

Posted: Tue Nov 23, 2010 5:56 pm
by hapazoid
Thanks for the quick suggestions.

I changed IsSet to isset and I add session_start(); but neither worked.

Re: Very simple problem

Posted: Tue Nov 23, 2010 6:02 pm
by Pazuzu156
try fixing includes as well. Instead of: include 'my.php'; Use: include("my.php");

See if that helps.

Re: Very simple problem

Posted: Tue Nov 23, 2010 6:33 pm
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.

Re: Very simple problem

Posted: Tue Nov 23, 2010 6:44 pm
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?

Re: Very simple problem

Posted: Tue Nov 23, 2010 9:33 pm
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?

Re: Very simple problem

Posted: Wed Nov 24, 2010 12:59 pm
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.

Re: Very simple problem

Posted: Wed Nov 24, 2010 3:01 pm
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.

Re: Very simple problem

Posted: Wed Nov 24, 2010 3:11 pm
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.

Re: Very simple problem

Posted: Wed Nov 24, 2010 6:08 pm
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.

Re: Very simple problem

Posted: Wed Nov 24, 2010 6:56 pm
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.

Re: Very simple problem

Posted: Wed Nov 24, 2010 11:05 pm
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.