Extra SSI Functions

Started by topleya, June 12, 2009, 09:00:17 AM

Previous topic - Next topic

topleya

Do you Have SMF? Do you want to personalize your homepage a bit to make it seem it is part of your forum? Well I can tell you how. First you need to go to http://{yoursite}/{forumdirectory}/ssi_examples.php then near the top it will say "To use SSI.php in your page add at the very top of your page before the <html> tag on line 1:" and under it is a PHP Require Code. Of course put it on any page where your going to use these codes. Now of course they give you some codes already, Like Recent posts, and Forum news, you know all that good stuff. I use that most of the time but there are some codes you may not know about. And I will show them t you and tell you what they do.

1) Only Allow admins
Code:

<?php
if (in_array(1$user_info['groups']))
{
echo 
"{PUT YOUR HTML OR WHATEVER HERE}";
}
?>


This PHP Code above Only allows Forum administrators to view that Page. You code through in an else staement like this:
Code:

<?php
if (in_array(1$user_info['groups']))
{
echo 
"{PUT YOUR HTML OR WHATEVER HERE}";
}
else 
{
echo
" Hey only admins can view this!";
}
?>


These Admin Codes are great if you have some kind of code that you wouldnt want anyone else but admins to access.

Now if you used include('whatever'); above on those Administrators only pages and want extra protection then on that page your including just add:
Code:

<?php
if (in_array(1$user_info['groups']))
{
echo 
"THE CODE(S)";
}
?>


to the page with the code where it says THE CODE. Now there is no back door to accessing that file. Even if they Directly accessed the file it wouldnt work. Even if you were a forum admin it wouldnt work because you dont have the REQUIRE statement on that file your including. Sorry Im probably being really confusing right now.

2)Registered Users only Pages
Maybe there is some information you dont want unregistered users to get to? Or maybe hide tings from Search bots?
Well Simply put this code on the page you want to protect(lets through in an else statement too):
Code:

<?php if ($context['user']['is_logged'])
{
echo
"YOUR HTML HERE";
}
else
{
echo
"Only registered users may view this page!";
}
?>


Now for crazy reason if you only want a page viewable by guests just change 'is_logged' to 'is_guest'

Now since you have pages only accessable by registered users you may want the links to those pages only viewable by registered users too... Now of course quite simple but I will just show you just incase :wink:
Code:

<?php if ($context['user']['is_logged'])
{
echo 
"<a href='LINK'>LINK NAME</a>";
}
?>


You never knew you could do these kind of things with SMF's SSI did you? Its neat, useful, and just plain Cool :lol:

3) Some Random Codes
This Code will say the Users forum name:
Code:

<?php echo ($context['user']['name']); ?>

What if the person is a guest? Easy:
Code:

<?php if (($context['user']['name']) = null))
{
echo 
"Guest";
}
else 
{
echo (
$context['user']['name']);
}
?>



Want to output how many posts a user has? Use this code then:
Code:

<?php echo ($user_info['posts']); ?>

We can also block a page so they have to have a certain amount of posts to access it.
Code:

<?php 
if(($user_info['posts']) >= {Posts they Need})
{
echo
"HTML Code";
}
else
{
$togo = {Posts they Need} - $user_info['posts'];

echo 
"<p align=center>Sorry you need {Posts they Need} posts to access this page and you have "$user_info['posts'] ." posts and you have " $togo ." posts(s) to go.</p>";
}
?>


Just fill in the amount of posts you want them to have before they can access the page. If they dont have enough posts it will say You need {amount of posts} you have {Posts they Have} you have {number of posts to go}

You can output what membergroup someone is in with this:
Code:

<?php if (($user_info['groups']['0']) == 1)

echo 
"Administrator";
}
elseif((
$user_info['groups']['0']) == 2)
{
echo 
"Global Moderator";
}
elseif((
$user_info['groups']['0']) == 0)
{
echo 
"Regular Member";
}
?>



Output how many total messages a user has:
Code:

<?php echo $context['user']['messages']; ?>


Output how many new messages a user has:
Code:

<?php echo $context['user']['unread_messages']; ?>


Show the users avatar:
Code:

<?php if(($context['user']['avatar']['href']) == null)
{
echo 
"<img src='URL to A Default Avatar' align='left'>";
}
else
{
echo 
"<img src='", ($context['user']['avatar']['href']), "' align='left'>";
}
?>


You will need to specify a URL to an avatar if the user doesnt have one set :wink:

Echo the forum name:
Code:

<?php echo $mbname?>


Echo the board URL:
Code:

<?php echo $boardurl?>


The forum webmaster email:
Code:

<?php echo $webmaster_email?>


SMF Version:
Code:

<?php echo ($modSettings['smfVersion']); ?>



Forum news(One at the top of the forum):
Code:

<?php echo ($modSettings['news']); ?>


Most online ever:
Code:

<?php echo ($modSettings['mostOnline']); ?>


Most online today:
Code:

<?php echo ($modSettings['mostOnlineToday']); ?>


Newest member:
Code:

<?php echo ($modSettings['latestRealName']); ?>


Output user email:
Code:

<?php echo ($context['user']['email']); ?>


Total forum posts:
Code:

<?php echo ($common_stats['total_posts']); ?>


Total Topics:
Code:

<?php echo ($common_stats['total_topics']); ?>


Total Members:
Code:

<?php echo ($common_stats['total_members']); ?>


Users IP:
Code:

<?php echo ($user_info['ip']); ?>


Cookie Name:
Code:

<?php echo $cookiename?>


Attachment Size allowed:
Code:

<?php echo $modsettings['attachmentSizeLimit']; ?>


Attachment Directory:
Code:

<?php echo $modSettings['attachmentUploadDir']; ?>


Attachment Extensions allowed:
Code:

<?php echo $modSettings['attachmentExtensions']; ?>


SMTP Host:
Code:

<?php echo $modSettings['smtp_host']; ?>


SMTP Port:
Code:

<?php echo $modSettings['smtp_port']; ?>


SMTP Username:
Code:

<?php echo $modSettings['smtp_username']; ?>


SMTP Password:
Code:

<?php echo $modSettings['smtp_password']; ?>


Time Offset:
Code:

<?php echo $modSettings['time_offset']; ?>


Members ID:
Code:

<?php echo $context['user']['id']; ?>


Members Session ID:
Code:

<?php echo $sc?>


Current Time:
Code:

<?php echo $current_time?>


A users Time Offset:
Code:

<?php echo $user_info['time_offset']; ?>


Here are a few functions that you may find very useful!

Parse BBCode:
Code:

<?php
parse_bbc
($msg);
?>


Replace $msg with a variable or a string, this will parse any type of BBCode that an SMF forum would (EG: This one) it also parses smileys :)
NOTE: Doesn't turn code into HTML Entities...


Censor Text:
Code:

<?php
censorText
($str);
?>


It obviously censors anything that you to have censored that you have set in the Admin CP.
NOTE: You have to do this function by itself, you cant have another function working on it at the same time! (EG: function(censorText($str)); won't work!)

SMF's MySQL Query:
Code:

<?php
db_query
("QUERY",__FILE__,__LINE__);
?>


This acts like mysql_query() only, it counts queries, and logs errors (hence the __FILE__ and __LINE__) other then that treat it as you would mysql_query(); ;)

Shorten Subject Function:
Code:

<?php
$str 
shorten_subject(STRINGLENGTH);
?>


Replace STRING with a you guessed it, STRING! and LENGTH with how long it should be ;) also, you then need to have it saved to a variable like $str or like echo shorten_subject(STRING, LENGTH);

Writing to online log:
Code:

<?php writeLog(true); ?>

This forces the update of information stored in the log_online table for the Whos Online (NOTE: I have personally used this function)

Time Format:
Code:

<?php
$time 
timeformat(TIME);
?>


This is a great function, and I use it all the time. Simply put in a Unix time stamp (like using PHP's time (http://php.net/time) function) and outputs a formated time like this: January 01, 1970, 09:00:11 AM :D
(Bet you never knew SMF has a [time] BBCode now did you? :P)

URL Exists:
Code:

<?php
$exist 
url_exists(URL);
?>


This function checks to see if the URL actually exists (checks for a HTTP 200 Status) much like the PHP file_extists function, only for addresses :P fsockopen() must be enabled on your account for this to work ;)

Caching:
Code:

<?php
cache_put_data
(NAME, ARRAY/NULLTTL);
?>


This function is SMF 2.x ONLY. Replace NAME with the name of the data being stored (to get it back later), ARRAY/NULL, if you do ' ', NULL, or nothing, it will erase the information, or have an array in it and it will store that data in the array :) TTL, Time to Live, just like DNS TTL, if you enter 60, the data will expire in 60 seconds (1 minute) the default is 120 seconds (or 2 minutes)

Now getting it out:
Code:

<?php
$cache 
cache_get_data(NAMETTL);
?>


NAME is the NAME you put the information in with, also, TTL is where you can reset how long it stays stored. Also, the return is either the array you put in OR returns NULL, either that it is their, just no data is stored OR that the cache of what you are wanting no longer exists :) ALSO SMF 2.x only ;) of course...

Hope these are of help to you, I've been using many on my site - http://www.tropico3.co.uk

Dzonny

great text, thank for this... :P
Think that this should go to tips and tricks section.... :)

npereira

Im running 2.0RC1-1,
How can I output members of a group.

Arantor

Quote from: npereira on July 07, 2009, 03:57:58 PM
Im running 2.0RC1-1,
How can I output members of a group.

Inside the forum or outside it?

npereira

inside the forum, like in a sidepanel... I use SimplePortal

jollyrogue

hey, does anyone know how i can use this to display a specific users name?

I would want to show

Username: <?php echo ($context['user']['name']); ?>
Posts: <?php echo ($user_info['posts']); ?>

but not the username and posts of the user looking at the page but of a specific member. Is this possible?

This page is awesome, it could seriously help me a lot on my site.

Arantor

Do you know the id of that member?

jollyrogue

yeah i will know the id of the member

for the sake of argument, lets say i want to show user id 2

Arantor

Then your best bet will be to call loadMemberData(2);

It'll populate a ton of stuff into $user_profile[2] for you.

aw06

i'm trying to call in this to get total members ...

<?php echo ($common_stats['total_members']); ?>

But i'm not seeing anything
:: ShopinJA.com Powered by SMF 1.1.19 | Ig-Oh Theme by Koni | 70 Rock Solid Error Free Mods | Many Custom Edits & Tweaks ::
- Host Unlimited Websites - Free Website Builder & Templates - Unlimited Disk Space & Bandwidth

IchBin™

I believe you are actually looking for $context['total_members']
IchBin™        TinyPortal

aw06

Quote from: IchBin™ on October 01, 2009, 04:12:09 PM
I believe you are actually looking for $context['total_members']

OK, will check

is it also possible to show who many guest and users are online, but not show they usernames ??
:: ShopinJA.com Powered by SMF 1.1.19 | Ig-Oh Theme by Koni | 70 Rock Solid Error Free Mods | Many Custom Edits & Tweaks ::
- Host Unlimited Websites - Free Website Builder & Templates - Unlimited Disk Space & Bandwidth

aw06

Tried it and it doesn't work ::)
:: ShopinJA.com Powered by SMF 1.1.19 | Ig-Oh Theme by Koni | 70 Rock Solid Error Free Mods | Many Custom Edits & Tweaks ::
- Host Unlimited Websites - Free Website Builder & Templates - Unlimited Disk Space & Bandwidth

Arantor

You may be able to use $modSettings['totalMembers'].

IchBin™

It'll work if you're including SSI I just tested it.
IchBin™        TinyPortal

aw06

Quote from: Arantor on October 01, 2009, 08:30:13 PM
You may be able to use $modSettings['totalMembers'].

brilliant, that works, once again to the rescue  8)
:: ShopinJA.com Powered by SMF 1.1.19 | Ig-Oh Theme by Koni | 70 Rock Solid Error Free Mods | Many Custom Edits & Tweaks ::
- Host Unlimited Websites - Free Website Builder & Templates - Unlimited Disk Space & Bandwidth

aw06

ooo boyyy  >:(

these dont work  ::)

<?php echo ($common_stats['total_posts']); ?>

<?php echo ($modSettings['totalPosts']); ?>

<?php echo ($context['total_posts']); ?>
:: ShopinJA.com Powered by SMF 1.1.19 | Ig-Oh Theme by Koni | 70 Rock Solid Error Free Mods | Many Custom Edits & Tweaks ::
- Host Unlimited Websites - Free Website Builder & Templates - Unlimited Disk Space & Bandwidth

Arantor

You didn't tell me you wanted that :P

Try $modSettings['totalMessages']

Oh, and $modSettings['totalTopics'] for the set.

aw06

Quote from: Arantor on October 01, 2009, 09:33:57 PM
You didn't tell me you wanted that :P

Try $modSettings['totalMessages']

Oh, and $modSettings['totalTopics'] for the set.

Bingo .... had gotton the totalTopics ....

How comes the ones posted dont work ?? or they work in older versions of smf
:: ShopinJA.com Powered by SMF 1.1.19 | Ig-Oh Theme by Koni | 70 Rock Solid Error Free Mods | Many Custom Edits & Tweaks ::
- Host Unlimited Websites - Free Website Builder & Templates - Unlimited Disk Space & Bandwidth

king kratos

My php send mail function doesn't work- mail(). I have been trying to code smtp for a page outside of my forum with no luck.

I tried using the smtp host, port, username, and password that was included in this topic. I cannot seem to get it to work either.

My question, when i call all four smtp codes into my page, do i use mail() still? This is how I've been trying to do it, but it doesn't work. As I am still learning how to code with php, some of these functions seem more difficult than they turn out to be.

Please help.

Thank you,
Kratos

Advertisement: