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

[syntax=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>[/syntax]
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Quick help plz

Post by ExtremeGaming »

[syntax=php]<?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
}
?>[/syntax]

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"][syntax=php]<?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
}
?>[/syntax]

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

[syntax=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>[/syntax]
<?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

[syntax=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>[/syntax]


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.
[syntax=php]echo "Welcome $_SESSION['username']";[/syntax]

Change it to
[syntax=php]echo "Welcome ".$_SESSION['username']." ";[/syntax]
<?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

[syntax=php]echo "Welcome", $_SESSION['username'];[/syntax]
which is generally a bit faster, not that you will notice it on a real site but worth mentioning :P
Image
Post Reply