Page 1 of 1

Sessions not working... I think

Posted: Mon Apr 04, 2011 5:34 pm
by danwguy
I know that that means I probably forgot a ; or } somewhere but I can't seem to find it anywhere in there. Can someone please take a glance at this and see if you can see what I'm missing, please. My eyes and head hurt from staring at the screen for days writing this stuff and it sometimes helps to have a spare set of eyes look at it. So without further adu, here's the code...

Code: Select all

<?php
session_start();

require("config.php");

$con = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Unable to connect to db: " .mysql_error());

$sel = mysql_select_db(DATABASE) or die("Unable to select database: " .mysql_error());

$errors = array();

if($_POST['username'] == '') {
	$errors[] = "You forgot to enter a username";
	}
if($_POST['password'] == '') {
	$errors[] = "You forgot to enter a password";
	}

function clean($str) {
		$str = @trim($str);
		if(get_magic_quotes_gpc()) {
			$str = stripslashes($str);
		}
		return mysql_real_escape_string($str);
	}

$username = clean($_POST['username']);
$pass = clean($_POST['password']);
$password = md5($pass);


$query = mysql_query("SELECT * FROM users WHERE username='$username' and pass='$password'");
if(!$query) {
	$errors[] = "Could not SELECT from the database: " .mysql_error();
	}
	if(empty($errors)) {
	$num = mysql_num_rows($query);
		if($num > 0) {
			$member = mysql_fetch_assoc($query);
			$_SESSION['username'] = $member['username'];
			$_SESSION['first_name'] = $member['first_name'];
			header("location: member-home.php");
			exit();
			} else {
				echo "Sorry but that username is not registered with our system<br />";
				echo "Please use <a href='register.php'>this</a> form to register";
				echo "<br /> Or go back and input a registered username";
				}
		} else {
			echo "Please go back and fix the following errors:<br />";
			foreach($errors as $msg) {
			echo " - " .$msg. "<br />";
		}

			
?>
Thank you in advance for any help.

Re: unexpected $end error

Posted: Mon Apr 04, 2011 6:00 pm
by TonsOfFun
danwguy wrote: } else {
echo "Please go back and fix the following errors:<br />";
foreach($errors as $msg) {
echo " - " .$msg. "<br />";
}


?>[/syntax]
You didn't close the foreach loop.
As you guessed, missing the '}'

Re: unexpected $end error

Posted: Mon Apr 04, 2011 6:02 pm
by danwguy
good sir, you are a genius and scholar! Thank you so much, I can't believe I missed that.

Re: unexpected $end error

Posted: Mon Apr 04, 2011 6:09 pm
by danwguy
ok part 2 now :( I ran through it and tried to log in, it goes to my member-home.php but shows as not logged in... here's the member-home.php...

Code: Select all

<?php
session_start();
if(isset($_SESSION['username'])) {
	echo "You have successfully logged in, congrats.";
	} else {
		echo "Script not working, check sessions";
		}
?>
but I get a page that says "Script not working, check sessions" why would that be? I set the $_SESSION['username'] variable in login-exec page that I posted above, why would it not carry over to this page?

Re: unexpected $end error

Posted: Mon Apr 04, 2011 6:30 pm
by TonsOfFun
Let's try a little debugging.

On the login page, change this code.

Code: Select all

                      $member = mysql_fetch_assoc($query);
                        $_SESSION['username'] = $member['username'];
                        $_SESSION['first_name'] = $member['first_name'];
                        header("location: member-home.php");
                        exit();
Comment out the header() and print_r the session array so it looks like this:

Code: Select all

      $member = mysql_fetch_assoc($query);
                        $_SESSION['username'] = $member['username'];
                        $_SESSION['first_name'] = $member['first_name'];
                        //header("location: member-home.php");
                        print_r($_SESSION);
                        exit();
This will print out the array and you can check and make sure the variables are being entered how you want them.
If this looks ok, print out the array on your member-home page

Re: unexpected $end error

Posted: Mon Apr 04, 2011 6:32 pm
by gooney0
The problem is probably with your $member array. I suspect your array is actually:

$member[0]["username"]

not

$member["username"]

You can verify this by adding in:

Code: Select all

print "<pre>";
print_r($member);
print "</pre>";
// or
print "<pre>";
print_r($_SESSION);
print "</pre>";
This is a great way to find these little mistakes.

Re: Sessions not working... I think

Posted: Mon Apr 04, 2011 6:34 pm
by danwguy
Changed it to print_r($_SESSION); and it outputs correct array... "Array ( [username] => danwguy [first_name] => Robert )" on the login-exec.php page

EDIT: when I print_R($_SESSION); on the member-home.php after session_start(); it prints "array()" why isn't the $_SESSION carrying over?

Re: Sessions not working... I think

Posted: Mon Apr 04, 2011 6:41 pm
by TonsOfFun
Are you working on your local machine? Or on a hosted server?

Re: Sessions not working... I think

Posted: Mon Apr 04, 2011 6:46 pm
by danwguy
hosted server... ipower.com

Re: Sessions not working... I think

Posted: Mon Apr 04, 2011 6:48 pm
by TonsOfFun
You have to specify where your sessions will be stored.
Use their live chat and they should be able to work it out.

Re: Sessions not working... I think

Posted: Mon Apr 04, 2011 6:49 pm
by danwguy
I created a folder in the root called php_sessions but maybe they have to point the sessions to that folder?

Re: Sessions not working... I think

Posted: Mon Apr 04, 2011 6:56 pm
by TonsOfFun

Re: Sessions not working... I think

Posted: Mon Apr 04, 2011 8:49 pm
by higibigi
Yep, I used to work for them Ipower. By default the php.ini file does not have a path set for the session_save_path. You should be able to go into your Control Panel under Scripting and Addons and PHP scripting and set the sessions to be stored to a location on your account.

Re: Sessions not working... I think

Posted: Tue Apr 05, 2011 11:50 am
by danwguy
Worked like a charm, Thank you