View Full Version : PHP Session Problemo.

03-20-2009, 09:19 PM
Right, this is gonna be abit confussing and hard to explain but here we go.

I'm trying to create a usercp, and i need to get the users ID when they login, to do this im setting a session when they login heres the bit of code..

if(mysql_num_rows($que) == 1)
$_SESSION['loggedin'] = 1;
$_SESSION['username'] = $_POST['username'];
header("Location: usercp.php");

and then i try and call it in my usercp.php with this bit of code

$sql = "SELECT `id` WHERE"
."`username` = '$username'"
."AND `password` = '$password'";
$res = mysql_query($sql);

$username = $_SESSION['username'];

then i would do

<?php echo $username; ?>

but doesnt echo the username, it echo's nothing at all and i really dont know why maybe some can explain and help me!

Thanks, Kevin

03-20-2009, 09:30 PM
Are you calling session_start() before anything else on each page using sessions?

Also, in your second code block you'll need to declare $username before the query so that $username will have a value when the query is executed/the string is created.

03-20-2009, 09:39 PM
Woot, it works great!

but im getting errors i have an file included

<?php include "includes/private/head.php"; ?>

and it gives me these errors

Notice: Undefined variable: keywords in /home/youronli/public_html/includes/private/head.php on line 14

Notice: Undefined variable: description in /home/youronli/public_html/includes/private/head.php on line 15

Notice: Undefined variable: title in /home/youronli/public_html/includes/private/head.php on line 16

Notice: Undefined variable: password in /home/youronli/public_html/usercp.php on line 18

03-20-2009, 10:01 PM
I believe that is PHP just being picky, change your error reporting settings in your PHP INI file to ignore notices.

03-20-2009, 10:12 PM
Well, they are only notices and, as such, you *could* change your reporting level to ignore them, IMO it would be better to find the cause and correct it rather than sweeping it under the rug.

Without seeing what's in includes/private/head.php this is just a guess, but it's likely that your code is attempting to use those variables before they've been set. If that's the case, it would be better to check to see if they are set before attempting to use them.

But, for more specific suggestions, you'd have to post the contents of includes/private/head.php.