Session variable in string

Post here is you are having problems with any of the tutorials.
Post Reply
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Session variable in string

Post 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>
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Quick help plz

Post 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.
<?php while(!$succeed = try()); ?>
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Re: Quick help plz

Post 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'];
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Quick help plz

Post 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>
<?php while(!$succeed = try()); ?>
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Re: Quick help plz

Post 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
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Quick help plz

Post 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']." ";
<?php while(!$succeed = try()); ?>
User avatar
TehCrayz
Posts: 38
Joined: Mon Dec 03, 2012 7:30 pm
Location: london
Contact:

Re: Quick help plz

Post by TehCrayz »

Thanks, you are literally 'AWESOME'. ;) ;) ;) ;) ;)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Quick help plz

Post 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
Image
Post Reply