Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    97
    Thanks
    10
    Thanked 0 Times in 0 Posts

    PHP Session Problemo.

    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..

    PHP Code:
    if(mysql_num_rows($que) == 1
        {
            
    $_SESSION['loggedin'] = 1;
            
    $_SESSION['username'] = $_POST['username'];
            
    header("Location: usercp.php");
        }else 
    and then i try and call it in my usercp.php with this bit of code

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

    $username $_SESSION['username']; 
    then i would do

    PHP Code:
    <?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

  • #2
    Regular Coder
    Join Date
    May 2006
    Posts
    152
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.

  • #3
    New Coder
    Join Date
    Mar 2009
    Posts
    97
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Woot, it works great!

    but im getting errors i have an file included

    PHP Code:
    <?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
    Last edited by Kev0121; 03-20-2009 at 08:46 PM.

  • #4
    Regular Coder
    Join Date
    May 2006
    Posts
    152
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I believe that is PHP just being picky, change your error reporting settings in your PHP INI file to ignore notices.

  • #5
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    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.
    John


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •