Sessions not working... I think

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

Post Reply
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Sessions not working... I think

Post 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.
Last edited by danwguy on Mon Apr 04, 2011 6:30 pm, edited 1 time in total.
TonsOfFun
Forum Commoner
Posts: 54
Joined: Wed Jun 02, 2010 7:37 pm

Re: unexpected $end error

Post 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 '}'
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: unexpected $end error

Post by danwguy »

good sir, you are a genius and scholar! Thank you so much, I can't believe I missed that.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: unexpected $end error

Post 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?
TonsOfFun
Forum Commoner
Posts: 54
Joined: Wed Jun 02, 2010 7:37 pm

Re: unexpected $end error

Post 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
gooney0
Forum Commoner
Posts: 56
Joined: Fri Jan 21, 2011 1:40 pm
Location: Reston, VA

Re: unexpected $end error

Post 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.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Sessions not working... I think

Post 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?
TonsOfFun
Forum Commoner
Posts: 54
Joined: Wed Jun 02, 2010 7:37 pm

Re: Sessions not working... I think

Post by TonsOfFun »

Are you working on your local machine? Or on a hosted server?
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Sessions not working... I think

Post by danwguy »

hosted server... ipower.com
TonsOfFun
Forum Commoner
Posts: 54
Joined: Wed Jun 02, 2010 7:37 pm

Re: Sessions not working... I think

Post 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.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Sessions not working... I think

Post by danwguy »

I created a folder in the root called php_sessions but maybe they have to point the sessions to that folder?
TonsOfFun
Forum Commoner
Posts: 54
Joined: Wed Jun 02, 2010 7:37 pm

Re: Sessions not working... I think

Post by TonsOfFun »

higibigi
Forum Newbie
Posts: 6
Joined: Fri Apr 01, 2011 12:56 pm
Location: Canada

Re: Sessions not working... I think

Post 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.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Sessions not working... I think

Post by danwguy »

Worked like a charm, Thank you
Post Reply