Page 1 of 1

Session variable in string

Posted: Sun Dec 09, 2012 8:49 pm
by TehCrayz
Hey, i want to do something like. for example. a popup bar appears and if i'm logged in, it would say welcome $_SESSION['username'] and if i'm not, then it will say some stuff bout signing up. anyways how am i able to make it say the username in the echo''; i've tried a lot of thins and now i'm out of ideas
<script type="text/javascript">
<!--Invocation code-->

var infobar=new informationbar()
infobar.setContent('<?php if ($_SESSION['username']){
	echo 'Welcome $_SESSION['username']';
}else{
	echo 'Welcome to GameCrayz! Please log in. Need to register? Click <a href="register.php">Here</a>!';
}  ?>')
infobar.setfrequency('session') 
infobar.initialize()

</script>

Re: Quick help plz

Posted: Sun Dec 09, 2012 8:55 pm
by ExtremeGaming
<?php
if(isset($_SESSION['username'])) {
?>
<script type="text/javascript">
window.alert("Welcome <?php echo $_SESSION['username']; ?>!");
</script>
<?php
} else {
?>
<script type="text/javascript">
window.alert("Welcome to GameCrayz! Please log in. Need to register? Go to the register page!");
</script>
<?php
}
?>
Links don't work in popups.

Re: Quick help plz

Posted: Sun Dec 09, 2012 9:08 pm
by TehCrayz
[quote="ExtremeGaming"]
<?php
if(isset($_SESSION['username'])) {
?>
<script type="text/javascript">
window.alert("Welcome <?php echo $_SESSION['username']; ?>!");
</script>
<?php
} else {
?>
<script type="text/javascript">
window.alert("Welcome to GameCrayz! Please log in. Need to register? Go to the register page!");
</script>
<?php
}
?>
Derp, oops i ment a sort of banner at the top, not a actual popup.
http://gyazo.com/5af6fcb2b2c7a1891132be25d36008a9

I Just need help with this... thanks...

echo 'Welcome '$_SESSION['username'];

Re: Quick help plz

Posted: Sun Dec 09, 2012 9:30 pm
by ExtremeGaming
Single quoted variables don't get parsed in php
<script type="text/javascript">
<!--Invocation code-->
 
var infobar=new informationbar()
infobar.setContent('<?php if ($_SESSION['username']){
        echo "Welcome $_SESSION['username']";
}else{
        echo 'Welcome to GameCrayz! Please log in. Need to register? Click <a href="register.php">Here</a>!';
}  ?>')
infobar.setfrequency('session')
infobar.initialize()
 
</script>

Re: Quick help plz

Posted: Sun Dec 09, 2012 9:39 pm
by TehCrayz
ExtremeGaming wrote:Single quoted variables don't get parsed in php
<script type="text/javascript">
<!--Invocation code-->
 
var infobar=new informationbar()
infobar.setContent('<?php if ($_SESSION['username']){
        echo "Welcome $_SESSION['username']";
}else{
        echo 'Welcome to GameCrayz! Please log in. Need to register? Click <a href="register.php">Here</a>!';
}  ?>')
infobar.setfrequency('session')
infobar.initialize()
 
</script>
i did that and i got an error :cry:
http://gyazo.com/d1bed48f67f19381ae2fe71e0da917bf

Re: Quick help plz

Posted: Sun Dec 09, 2012 9:57 pm
by ExtremeGaming
My bad I should've expected that :oops:

This should be the error.
echo "Welcome $_SESSION['username']";
Change it to
echo "Welcome ".$_SESSION['username']." ";

Re: Quick help plz

Posted: Sun Dec 09, 2012 10:04 pm
by TehCrayz
Thanks, you are literally 'AWESOME'. ;) ;) ;) ;) ;)

Re: Quick help plz

Posted: Sun Dec 09, 2012 10:06 pm
by jacek
You can also avoid having to join two strings together by doing
echo "Welcome", $_SESSION['username'];
which is generally a bit faster, not that you will notice it on a real site but worth mentioning :P