...

View Full Version : Session vars not echoing



amcf1992
09-29-2011, 03:44 PM
<?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>

sunfighter
09-29-2011, 06:47 PM
After removing the redundancies in your script, I see no problem


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

amcf1992
09-29-2011, 07:45 PM
thanks for your reply, but how would i session row data from mysql?

sunfighter
09-29-2011, 09:15 PM
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:
<?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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum