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

    Exclamation Session vars not echoing

    PHP Code:
    <?php 
    session_start
    (); 
    $_SESSION['id'] = $row['id'];  
    $_SESSION['firstname'] = $row['firstname'];  
    $_SESSION['lastname'] = $row['lastname'];  
    $_SESSION['address'] = $row['address']; 
            
    ?>
    <html xmlns="http://www.w3.org/1999/xhtml">  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    <LINK href="includes/css/style.css" rel="stylesheet" type="text/css">
    <title>Account Dashboard</title>  
    </head>  
    <body>
    <?php
    include('header.php');

    include(
    'dbsettings.php');

    //include('navbar.php');


    $con mysql_connect("$host","$user","$password");  
    if (!
    $con)  
      {  
      die(
    'Could not connect: ' mysql_error());  
      }  

    mysql_select_db("$db_name"$con);  
    $username$_row['username']; // Editted
    $sql="SELECT * FROM `user` WHERE `username`='{$username}'";  

    $result mysql_query("$sql");
     if(
    mysql_fetch_array($result))
     {
    echo 
    "ID:";       
    echo 
    $_SESSION['id'];
    echo 
    "<BR />";
    echo 
    "First Name:"
    echo 
    $_SESSION['firstname'];
    echo 
    "<BR />";
    echo 
    "Last Name:"
    echo 
    $_SESSION['lastname'];
    echo 
    "<BR />";
    echo 
    "Address:"
    echo 
    $_SESSION['address'];

    }
        
    ?>
     
     

      </div>
      
      <p class="logout">
      <a href=logout.php>Logout</a>
      </p>
      
    <?php 
    session_start
    (); 
    $_SESSION['id'] = $row['id'];  
    $_SESSION['firstname'] = $row['firstname'];  
    $_SESSION['lastname'] = $row['lastname'];  
    $_SESSION['address'] = $row['address']; 
            
    ?>
    <html xmlns="http://www.w3.org/1999/xhtml">  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    <LINK href="includes/css/style.css" rel="stylesheet" type="text/css">
    <title>Account Dashboard</title>  
    </head>  
    <body>
    <?php
    include('header.php');

    include(
    'dbsettings.php');

    //include('navbar.php');


    $con mysql_connect("$host","$user","$password");  
    if (!
    $con)  
      {  
      die(
    'Could not connect: ' mysql_error());  
      }  

    mysql_select_db("$db_name"$con);  
    $username$_row['username']; // Editted
    $sql="SELECT * FROM `user` WHERE `username`='{$username}'";  

    $result mysql_query("$sql");
     if(
    mysql_fetch_array($result))
     {
    echo 
    "ID:";       
    echo 
    $_SESSION['id'];
    echo 
    "<BR />";
    echo 
    "First Name:"
    echo 
    $_SESSION['firstname'];
    echo 
    "<BR />";
    echo 
    "Last Name:"
    echo 
    $_SESSION['lastname'];
    echo 
    "<BR />";
    echo 
    "Address:"
    echo 
    $_SESSION['address'];

    }
        
    ?>
     
     

      </div>
      
      <p class="logout">
      <a href=logout.php>Logout</a>
      </p>
      
      <p style="position: absolute; top: 100px; left: 230px; background-color: white; margin: 15px; "> Welcome, <?php echo "$username"?> This is your account:
      <?php

    mysql_close
    ($con); 
    include(
    'footer.php'
    ?>


    </body>  
    </html>

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,036
    Thanks
    23
    Thanked 589 Times in 588 Posts
    After removing the redundancies in your script, I see no problem

    Code:
    <?php
    session_start();
    $_SESSION['id'] = '56789';
    $_SESSION['firstname'] = 'tom';
    $_SESSION['lastname'] = 'bastic';
    $_SESSION['address'] = 'middle earth';
    ?>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <LINK href="includes/css/style.css" rel="stylesheet" type="text/css">
    <title>Account Dashboard</title>
    </head>
    <body>
    <?php
    $username= 'username'; // Editted
    echo "ID:";
    echo $_SESSION['id'];
    echo "<BR />";
    echo "First Name:";
    echo $_SESSION['firstname'];
    echo "<BR />";
    echo "Last Name:";
    echo $_SESSION['lastname'];
    echo "<BR />";
    echo "Address:";
    echo $_SESSION['address'];
     ?>
    <p class="logout">
    <a href=logout.php>Logout</a>
    </p>
    <p style="position: absolute; top: 100px; left: 230px; background-color: white; margin: 15px; ">
    Welcome, <?php echo "$username"; ?> This is your account:
    </body>
    </html>

  • #3
    Regular Coder
    Join Date
    Jul 2011
    Posts
    140
    Thanks
    8
    Thanked 0 Times in 0 Posts
    thanks for your reply, but how would i session row data from mysql?

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,036
    Thanks
    23
    Thanked 589 Times in 588 Posts
    I have to assume that we have already went through a page for log in that asked for the user name and password and that you next varified that these were correct and in so doing, got the ID (or what ever) that identifies the user in the database. You next called this page and sent it the ID via $_POST method. And now we start the page:
    Code:
    <?php
    session_start();
    $ID = $_POST["ID"];
    $query = "SELECT firstname, lastname, address, id FROM the_database WHERE ID = $ID";
    $result= mysql_query($query);
    
    while($row = mysql_fetch_array($result))
    {
    	$_SESSION["id"] = $row["id"];
    	$_SESSION["firstname"] = $row["firstname"];
    	$_SESSION["lastname"] = $row["lastname"];
    	$_SESSION["address"] = $row["address"];
    }
    
    echo $_SESSION["id"];
    echo $_SESSION["firstname"];
    echo $_SESSION["lastname"];
    echo $_SESSION["address"];
    
    ?>
    This may not be exactly what you want but it should give you a start and should answer your question. If not just ask what you need to know.


  •  

    Posting Permissions

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