Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

How to check if user is logged in?

edited January 2008 in Vanilla 1.0 Help
I've got a problem that's above my head and would appreciate some help. On http://idrinkyourmilkshake.com, I've embedded a sound file in menu.php, like this: <embed src="milkshake.mp3" align="absmiddle" autostart="true" width="225" height=16><noembed><bgsound src="milkshake.mp3" autostart="true" width="225" height=20></noembed><br> It's set to autoplay because people who come by the site the first time expect to hear the clip -- I really don't want to lose that. BUT as you can imagine, having the file play on every refresh gets really old when you're browsing and posting the discussions. So, I'm looking for a way to check if the user is logged in, and if so, I'd like to embed the same thing with autostart turned off. Can somebody tell me what the code would be for menu.php to check if the user is logged in and then display either version of the embed code? Thank you much!

Comments

  • You could write an extension for that, with a custom control.
    See how the banner works here.
    Somewhere in the Render function, you'll type:
    if ( $this->Context->Session->UserID == 0 ) {...}
  • Thank you Grahack. If I understand this correctly, it would have been smarter to use an extension because of upgrading woes, but since I am already using a custom theme on the site, I might as well stick with that? As for the code, I don't know much PHP at all, but I imagine it would have to look something like this: if ($this->Context->Session->UserID > 0) { echo '<embed src="milkshake.mp3" align="absmiddle" autostart="false" width="225" height=16><noembed><bgsound src="milkshake.mp3" autostart="false" width="225" height=20></noembed><br>'; } else { echo '<embed src="milkshake.mp3" align="absmiddle" autostart="true" width="225" height=16><noembed><bgsound src="milkshake.mp3" autostart="true" width="225" height=20></noembed><br>'; } Does that look right? I'd just try it out but the site is actually getting a good deal of traffic right now so I don't want to mess it up. I'd appreciate if somebody could tell me if this is going to work, or if I got the syntax all wrong. Do I have to escape the quotes? Thanks again!
  • edited January 2008
    To be sure, you'll have to try on a safe install! Maybe you'll find a guru around your place to install a Vanilla instance locally on your computer.
    If not, just paste your final code here, in html and between <code> tags.
    It should end like this (tested: your html code is echoed correctly):
    <?php /* Extension Name: Embed Sound For Guests Extension Url: http://lussumo.com/community Description: Embeds a sound file in pages viewed by guests. Version: 0.0.1 Author: Grahack Author Url: n/a */ // embed the sound only in the following pages $SoundPages = array( 'index.php', 'categories.php', 'comments.php', 'search.php' ); // name or path of the sound file, as in src="???" define( 'SoundFile', 'milkshake.mp3' ); if ( in_array($Context->SelfUrl, $SoundPages )) { class EmbedSound extends Control { function EmbedSound( &$Context ) { $this->Name = 'EmbedSound'; $this->Control( $Context ); } function Render() { if ($this->Context->Session->UserID > 0) $Guest = false; else $Guest = true; echo "\n"; echo '<embed src="'.SoundFile.'" align="absmiddle" autostart="'.($Guest?'true':'false').'" width="225" height=16 />'; echo "\n"; echo '<noembed><bgsound src="'.SoundFile.'" autostart="'.($Guest?'true':'false').'" width="225" height=20></noembed><br>'; echo "\n"; } } $EmbedSound = new EmbedSound($Context); $Page->AddRenderControl($EmbedSound, $Configuration["CONTROL_POSITION_HEAD"]+1); }
    I allowed myself to close the embed tag the short way (html validation).
    You have to configure the pages where you want to here the sound and the name of the file.
    If it works, I'll upload it for the community.
  • Thanks again, Grahack, this is amazing. I'm in a little bit over my head so I have a few dumb questions about how to use this. Does it work like the other extensions, i.e. I drop it in the extensions folder and activate through the settings page? How does the extension know where to put the embed -- is that control_position_head or do I have to somehow place it in the right spot? You mention my "final code" -- does that mean I have to make more changes to this? Sorry I know don't my around PHP better. I really appreciate you helping with this.
  • edited January 2008
    Well sorry, it was not very clear since I decided to code it entirely in the middle of my post .

    HOW
    Yes it's the content of a classical default.php file that you'll have to create in an EmbedSoundForGuests folder, under the extensions folder. Then settings, extensions, activation with the checkbox, as usual. You can go ahead, it shouldn't break your site, maybe you'll have to correct the path to your sound file.

    WHERE
    CONTROL_POSITION_HEAD + 1 seems to place the embed tag right after the body tag.

    FINAL CODE
    That was before I decided to code it. I left it because it's always (quite) true: people here can test little and simple bits of code for you.
    But the quickest way to test is to install a local server on your machine.

    So does it work well now?
  • Thanks, I'll try it in a second. Ideally, I'd like the embed to appear where it is now, underneath the banner and above the menu. Is there a way to make that happen?
  • Great! This seems to be working fine. When I activate the extension, the embed shows up in the top left corner of the page. It plays if I'm not logged in and doesn't when I am. Excellent. My only question would be the placement -- it's actually ok up there, but it might confuse people who are used to finding the embed under the banner. So, if there is an easy way to move it, I wouldn't mind, otherwise this will work for me, too. Thanks again, Grahack.
  • I think that +2 will work.
  • I tried +2 and +3, but they didn't seem to move it at all.
  • Oh, ok, you're talking about the 'menu', try CONTROL_POSITION_MENU + 1.
  • Almost perfect! http://idrinkyourmilkshake.com is live now with the extension, and the embed is *almost* where I want it. It's under the tab list now -- ideally it would be above, between the banner and the menu. But this is fine, and the extension works great. Thanks again to Grahack and the Vanilla support community! Here's the code I'm using now:

    <?php /* Extension Name: Embed Sound For Guests Extension Url: http://lussumo.com/community Description: Embeds a sound file in pages viewed by guests. Version: 0.0.1 Author: Grahack Author Url: n/a */ // embed the sound only in the following pages $SoundPages = array( 'index.php', 'categories.php', 'comments.php', 'search.php' ); // name or path of the sound file, as in src="???" define( 'SoundFile', 'milkshake.mp3' ); if ( in_array($Context->SelfUrl, $SoundPages )) { class EmbedSound extends Control { function EmbedSound( &$Context ) { $this->Name = 'EmbedSound'; $this->Control( $Context ); } function Render() { if ($this->Context->Session->UserID > 0) $Guest = false; else $Guest = true; echo "\n"; echo '<div align="center"><br><embed src="'.SoundFile.'" align="absmiddle" autostart="'.($Guest?'true':'false').'" width="225" height=16 />'; echo "\n"; echo '<noembed><bgsound src="'.SoundFile.'" autostart="'.($Guest?'true':'false').'" width="225" height=20></noembed></div>'; echo "\n"; } } $EmbedSound = new EmbedSound($Context); $Page->AddRenderControl($EmbedSound, $Configuration["CONTROL_POSITION_MENU"]+1); }
  • edited January 2008
    Try to tweak the position, $Configuration["CONTROL_POSITION_MENU"] - 1, or whatever.
    There's nothing bad that can happen!
  • In your settings.php, you'll find
    // Vanilla Control Positions $Configuration['CONTROL_POSITION_HEAD'] = '100'; $Configuration['CONTROL_POSITION_MENU'] = '200'; $Configuration['CONTROL_POSITION_BANNER'] = '200'; $Configuration['CONTROL_POSITION_PANEL'] = '300'; $Configuration['CONTROL_POSITION_NOTICES'] = '400'; $Configuration['CONTROL_POSITION_BODY_ITEM'] = '500'; $Configuration['CONTROL_POSITION_FOOT'] = '600'; $Configuration['CONTROL_POSITION_PAGE_END'] = '700';
    Vanilla controls are printed after being sorted. The extension we wrote together just adds a new control. You just have to find the right value.
This discussion has been closed.