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 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Posts
    140
    Thanks
    8
    Thanked 0 Times in 0 Posts

    not echoing correctly

    The echo is not echoing please help


    Code:
    <?php session_start(); ?>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Account Dashboard</title>
    </head>
    <body>
    <?php
    $name = $_SESSION['username'];  
    echo "$name";
    ?>
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,209
    Thanks
    7
    Thanked 268 Times in 267 Posts
    You're not giving us much to start with. Presumably you put something in $_SESSION['username'] somewhere, and now when you print it, it's not showing up?

    Dave

  • #3
    Regular Coder
    Join Date
    Jul 2011
    Posts
    140
    Thanks
    8
    Thanked 0 Times in 0 Posts
    This is the code where I have session variables


    Code:
    <?php
    include("dbsettings.php");
    
    
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // Get values from form
    
    $username=$_POST['username'];
    $firstname=$_POST['firstname'];
    $lastname=$_POST['lastname'];
    $address=$_POST['address'];
    $email=$_POST['email'];
    $password=$_POST['password'];
    $pass2=$_POST['password2'];
    $account=$_POST['account'];
    $secret1=$_POST['secret1'];
    
    $sql="select * from `" . $tbl_name . "` where `username`='" . $username . "' and `password`='" . $password . "' ";
    
    
    
    $result=mysql_query($sql);
    $count=mysql_num_rows($result);
    
    if($count==1)
    
    {
    
    
    $_SESSION['username'];
    $_SESSION['password'];
    
    }
     
    echo '<META HTTP-EQUIV="Refresh" Content="0; URL=account.php?success">'; 
    ?>

  • #4
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,209
    Thanks
    7
    Thanked 268 Times in 267 Posts
    Two things... I don't see a call to session_start() in that second routine, and then I see a random statement:
    Code:
    $_SESSION['username'];
    I'm guessing you mean:
    Code:
    $_SESSION['username'] = $username;
    Dave

  • #5
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    It looks like you're making a login script, but it's not really doing anything. Inside the count if, you should be assigning variables - not just stating them randomly. It should be $_SESSION['username'] = $variable etc. You really should know how variables work if you're attempting programming.

    Also, you don't need encapsulation when echo'ing variables in PHP. echo $name; will work just fine. Using encapsulation ("'s) like you're doing will also work, but takes longer as php takes it as a string, then parses the value for $name inside the string. While you're learning, you should note the difference between ' and ". Encapsulating text in ' will tell php to treat it as plain text, whereas encapsulating a string in " will tell php to parse any variables inside it as their value.
    An example of that:
    PHP Code:
    ;
    $foo 'bar';
    echo 
    $foo// Will echo bar;
    echo '$foo'// Will echo $foo;
    echo "$foo"// Will echo bar; 
    Also, look at the isset() and empty() functions for things like session vars.

  • #6
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by tracknut View Post
    Two things... I don't see a call to session_start() in that second routine, and then I see a random statement:
    Code:
    $_SESSION['username'];
    I'm guessing you mean:
    Code:
    $_SESSION['username'] = $username;
    Dave
    Oh, and session_start(). Never noticed that one haha.

    session_start() is required at the top of all pages that are going to use sessions.

  • #7
    Regular Coder
    Join Date
    Jul 2011
    Posts
    140
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I do have session start at the top


    Code:
    <?php session_start(); ?>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Account Dashboard</title>
    </head>
    <body>
    <?php
    
    $name = $_SESSION['username'];  
    echo "$name";
    ?>
    </body>
    </html>

  • #8
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Not in the script you pasted in post #3 you don't. The script you're setting (or trying to set in your original case) session variables. And please lads, use php tags instead of code tags.

  • #9
    New Coder
    Join Date
    Aug 2011
    Posts
    49
    Thanks
    1
    Thanked 0 Times in 0 Posts
    if those scripts are just a small part of yr complete code (that relates to this problem) then the problem may lie somewhere else. otherwise as what other posters have said, you are not passing any value into your Session[username] at all.

  • #10
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by capypara View Post
    if those scripts are just a small part of yr complete code (that relates to this problem) then the problem may lie somewhere else. otherwise as what other posters have said, you are not passing any value into your Session[username] at all.
    The problem is definitely what's above. You can see that there is no assignment to the session variables, and no session_start() at the top of his page.

    But, it's logical to assume that there could be errors in other files as well - just nothing that will relate to this immediate problem.


  •  

    Tags for this Thread

    Posting Permissions

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