Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-14-2011, 03:59 AM   PM User | #1
amcf1992
Regular Coder

 
Join Date: Jul 2011
Posts: 114
Thanks: 7
Thanked 0 Times in 0 Posts
amcf1992 is an unknown quantity at this point
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>
amcf1992 is offline   Reply With Quote
Old 09-14-2011, 04:06 AM   PM User | #2
tracknut
Regular Coder

 
Join Date: Aug 2006
Posts: 891
Thanks: 4
Thanked 205 Times in 204 Posts
tracknut is an unknown quantity at this point
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
tracknut is offline   Reply With Quote
Old 09-14-2011, 04:22 AM   PM User | #3
amcf1992
Regular Coder

 
Join Date: Jul 2011
Posts: 114
Thanks: 7
Thanked 0 Times in 0 Posts
amcf1992 is an unknown quantity at this point
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">'; 
?>
amcf1992 is offline   Reply With Quote
Old 09-14-2011, 04:31 AM   PM User | #4
tracknut
Regular Coder

 
Join Date: Aug 2006
Posts: 891
Thanks: 4
Thanked 205 Times in 204 Posts
tracknut is an unknown quantity at this point
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
tracknut is offline   Reply With Quote
Old 09-14-2011, 04:33 AM   PM User | #5
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
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.
BluePanther is offline   Reply With Quote
Old 09-14-2011, 04:34 AM   PM User | #6
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
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.
BluePanther is offline   Reply With Quote
Old 09-14-2011, 04:36 AM   PM User | #7
amcf1992
Regular Coder

 
Join Date: Jul 2011
Posts: 114
Thanks: 7
Thanked 0 Times in 0 Posts
amcf1992 is an unknown quantity at this point
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>
amcf1992 is offline   Reply With Quote
Old 09-14-2011, 04:51 AM   PM User | #8
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
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.
BluePanther is offline   Reply With Quote
Old 09-14-2011, 05:53 AM   PM User | #9
capypara
New Coder

 
Join Date: Aug 2011
Posts: 49
Thanks: 1
Thanked 0 Times in 0 Posts
capypara is an unknown quantity at this point
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.
capypara is offline   Reply With Quote
Old 09-14-2011, 06:40 AM   PM User | #10
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
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.
BluePanther is offline   Reply With Quote
Reply

Bookmarks

Tags
authentication, php, user

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:24 AM.


Advertisement
Log in to turn off these ads.