Parse Error..

Need some help…
The error message I’m getting is:

Parse error: syntax error, unexpected T_VARIABLE in /home/virtous/public_html/core/functions/users.php on line 22.

Line 22 is the 3rd line starting from the bottom.

[php]<?php
function user_exists($username) {
$username = sanitize($username);
return (mysql_result(mysql_query(“SELECT COUNT(user_id) FROM users WHERE username = ‘$username’”), 0) == 1) ? true : false;
}

function user_active($username) {
	$username = sanitize($username);
	return (mysql_result(mysql_query("SELECT COUNT(`user_id`)FROM `users` WHERE `username` = '$username' AND `active`='1'"), 0) == 1) ? true : false;
	}

function user_id_from_username($username) {
	$username = sanitize($username);
	return mysql_result(mysql_query("SELECT `user_id` FROM `users` WHERE `username` = '$username'"), 0, `user_id`);
}	
	

function login($username, $password) { 
	$user_id = user_id_from_username($username); 
	$username = sanitize($username); 
	$password = md5($password); 
	return (mysql_result(mysql_query("SELECT COUNT(user_id) FROM users WHERE username = '$username' AND password = '$password'"), 0) == 1) $user_id : false;
}

?>[/php]

Any help is appreciated!

//Virt

Missing question mark?

here:
== 1) ? $user_id : false;

Thank you!
I corrected that and it runs well except for when I enter a correct user name and password.
At the moment when I go to the index.php file it says “Not logged in”, which obviously is correct since I haven’t logged in yet. However, when I enter a valid user name and password, I want it to direct me to the index.php page and change the text to “Logged in”. So the problem I’m having now (if I understood correct) is that the session isn’t set and therefore the text isn’t changed.

This is the login.php page. When I click on the “Log in” button this page loads. It will make sure none of the fields are empty. Also checks so that the user name matches the password.
Look at lines 20-24 at the “else” (9 lines up starting from the bottom) , It should set the session and return me to the index page.
[php]<?php
include ‘core/init.php’;

if(empty($_POST) === false) {
$username = $_POST[‘username’];
$password = $_POST[‘password’];

if (empty($username) === true || empty($password)=== true) {
	$errors[] ='Incorrect username or password.';
} else if (user_exists($username) === false) {
	$errors[] ='Incorrect username or password.';
} else if(user_active($username) === false) {
	$errors[] = 'You haven\'t activated your account';
} else {
	$login = login($username, $password);
	
	if($login === false) {
		$errors[] = 'Incorrect username or password';
	} else {
		$_SESSION[`user_id`] = $login;
		header('Location: index.php');
		exit();
	}
}
print_r($errors);

}
?>[/php]

Once at the index.php page, the session should be set and the text should change to “Logged in”. For some reason it doesn’t. It only takes me to the index page but the text is still “Not logged in”.

This is my index.php file
[php]<?php
include ‘includes/overall/header.php’;
include ‘core/database/connect.php’;
?>

<?php if (isset($_SESSION[user_id])){ echo 'Logged in'; } else { echo 'Not logged in'; } ?>
	<div id="Main_Wrapper">
	</div>					
<?php include 'includes/overall/footer.php'; ?>[/php]

Any help is much appreciated!

//Virt

I cannot see where do you call function session_start()… Is it in the core/init.php ?

You need to call session_start() to create/resume session, at the top of each script where you work with session variables.

Yes, correct session_start() is inside init.php

And did you include this code in your index.php ?

Apparently I didn’t lol… I included init.php inside of index.php now and get these errors:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/virtous/public_html/includes/overall/header.php:5) in /home/virtous/public_html/core/init.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/virtous/public_html/includes/overall/header.php:5) in /home/virtous/public_html/core/init.php on line 2

Fixed it, had to place it above the other includes!!

And now the log in function works!! Thanks so much!

Sponsor our Newsletter | Privacy Policy | Terms of Service