...

View Full Version : not echoing correctly



amcf1992
09-14-2011, 03:59 AM
The echo is not echoing please help



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

tracknut
09-14-2011, 04:06 AM
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

amcf1992
09-14-2011, 04:22 AM
This is the code where I have session variables



<?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">';
?>

tracknut
09-14-2011, 04:31 AM
Two things... I don't see a call to session_start() in that second routine, and then I see a random statement:


$_SESSION['username'];


I'm guessing you mean:


$_SESSION['username'] = $username;


Dave

BluePanther
09-14-2011, 04:33 AM
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:

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

BluePanther
09-14-2011, 04:34 AM
Two things... I don't see a call to session_start() in that second routine, and then I see a random statement:


$_SESSION['username'];


I'm guessing you mean:


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

amcf1992
09-14-2011, 04:36 AM
I do have session start at the top



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

BluePanther
09-14-2011, 04:51 AM
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.

capypara
09-14-2011, 05:53 AM
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.

BluePanther
09-14-2011, 06:40 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum