Log in

View Full Version : Displaying logged in users username



Fehm
08-02-2009, 07:04 PM
Hi :)

Developing my PHP knowledge im making a radio panel, like i've said in another post, and ive looked for tutorials but cant find any on how to display a users username..

What i though it would be (And im a terrible coder so it wont work!, im coding to gain experience)


<?php

session_start();

if(!isset($_SESSION['logged_in'])
}

$username = $_SESSION['username'];
?>

Hello, <?= $username ?>! Welcome to my website.

Any help would be greatly appreciated.. Thank you :)
+rep if you can help...

Excellent2
08-02-2009, 07:13 PM
echo "$_SESSION['username']";

Dentafrice
08-02-2009, 07:15 PM
echo "$_SESSION['username']";
You don't need quotes around it..



Hello, <?php echo $_SESSION['username']; ?>! Welcome to our website.

Fehm
08-02-2009, 07:27 PM
You don't need quotes around it..



Hello, <?php echo $_SESSION['username']; ?>! Welcome to our website.


Thank you lots :)!

Now, if i where to put that in a seperate file and then added <? include(loggedin.php)?> it would work wouldnt it??

Sorry im still learning ^^

Dentafrice
08-02-2009, 08:00 PM
Yes it would. Please quit using short-tags though (<?) and use (<?php).



<?php include "loggedin.php"; ?>

Excellent2
08-02-2009, 08:06 PM
You don't need quotes around it..
I prefer to :)

Dentafrice
08-02-2009, 08:07 PM
Why? It's pointless and stupid.

Excellent2
08-02-2009, 08:08 PM
Why? It's pointless and stupid.Because I prefer to.

Dentafrice
08-02-2009, 08:19 PM
Because I prefer to.
Well, your preference is pointless.

You're basically telling PHP that you're going to output a string "", but you're actually outputting a variable inside that string, so you're making your program inefficient because it is having to check that string "" for variables ($_SESSION), so you're just putting more work on your program.

Compared to

Simple telling PHP you are going to output a variable, you save the time and hassle of having it look through the string for it.

Iszak
08-02-2009, 08:24 PM
I agree with Dentafrice about the fact that it is rather pointless but it should be noted that the added extra execution time is so negligible it shouldn't matter. But still it's a bad habit and should be avoided.

Excellent2
08-02-2009, 08:29 PM
Well, your preference is pointless.

You're basically telling PHP that you're going to output a string "", but you're actually outputting a variable inside that string, so you're making your program inefficient because it is having to check that string "" for variables ($_SESSION), so you're just putting more work on your program.

Compared to

Simple telling PHP you are going to output a variable, you save the time and hassle of having it look through the string for it.Well if I'm going to work on a little script I couldn't really care about what PHP has to do.

Dentafrice
08-02-2009, 08:42 PM
You should worry about it when it makes you look stupid, and look like a bad coder.

Excellent2
08-02-2009, 08:44 PM
You should worry about it when it makes you look stupid, and look like a bad coder.Which I'm not because PHP isn't my main language. It's something I casually use now and then. If I were to be some big time developer, perhaps I'd use proper methods.

Dentafrice
08-02-2009, 08:46 PM
Which I'm not because PHP isn't my main language. It's something I casually use now and then. If I were to be some big time developer, perhaps I'd use proper methods.
You should use proper methods when attempting to help someone, otherwise let someone who "use proper methods" help them out ;)

Excellent2
08-02-2009, 08:48 PM
You should use proper methods when attempting to help someone, otherwise let someone who "use proper methods" help them out ;)Regardless of using proper methods, I helped him. If he wants to learn the proper procedures then I'll let others help.

Fehm
08-02-2009, 08:48 PM
Look, You both helped me out loads... so theres no need to argue, Excellent thanks i understand now, and Dentafrice thanks for updating it and helping me learn a bit more on the syntax... You both contributed and helped alot sooo dont argue :P tehe thanks alot though :)

Want to hide these adverts? Register an account for free!