userbased content from session php - php

I'm looking to setup a group field for my custom user script. How can I go about only showing certain content to certain groups
Example:
admin (all content)
moderator (extended content)
user (user content)
guest (general overview)
4 group id's 0 - 4
4 banned
3 admin
2 moderator
1 user
0 guest
Example of the PHP in use
// Keep reminding the user this account is not active, until they activate
if($row['group_id'] == 4) { //display all
echo
'<div class="info">
This account is at risk of being banned Please obey the site rules.
</div>';
} else {
exit();
}
Example of the PHP MySQL session
/* Displays user information and some useful messages */
session_start();
// Check if user is logged in using the session variable
if ( $_SESSION['logged_in'] != 1 ) {
$_SESSION['message'] = "You must log in before viewing your profile page!";
header("location: http://localhost/login-system/error.php");
}
else {
// Makes it easier to read
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
$email = $_SESSION['email'];
$active = $_SESSION['active'];
$group_id = $_SESSION['group_id'];
}

You can do this with this example:
Lets say in this is a blog.
<?php
// 0 = Banned
// 1 = User
// 2 = Admin
if($_SESSION['group_id'] == 0) {
?>
<h1>You Are Banned</h1>
<?php
} elseif($_SESSION['group_id'] == 1) {
?>
<p>Welcome to my blog.</p>
<?php
} elseif($_SESSION['group_id'] == 2) {
?>
<p>Welcome to my blog.</p><button>Delete Post</button>
<?php
}
Or you can do this by a second method:
<p id="1">Welcome To My Blog!</p><button id="2">Delete Content</button>
<h1 id="3">You are banned from this website!</h1>
<?php
if($_SESSION['group_id'] == 0) {
echo '<style>
#2 {
display:none;
}
#3 {
display:none;
}
</style>';
} elseif ($_SESSION['group_id'] == 1) {
echo '<style>
#1 {
display:none;
}
#3 {
display:none;</style>';
} elseif ($_SESSION['group_id'] == 2) {
echo '<style>
#1 {
display:none;
}';
}
Hope this helps!

Related

Why is HTML in php function not visible in view source

function that runs when i want to edit a client status.
// deletes (sets inactive) client
function setClientStatus($client) {
if ($_GET['action'] == "setinactive") {
//database functions
$sql = 'UPDATE clients SET active="0" WHERE id="'. $client .'"';
$result = query($sql);
confirmQuery($result);
//set message to user to display
$_SESSION['error'] = false;
$msgs[] = "Client was set to inactive and removed from the list!";
$_SESSION['userMsg'] = $msgs;
}
if ($_GET['action'] == "setactive") {
//database functions
$sql = 'UPDATE clients SET active="1" WHERE id="'. $client .'"';
$result = query($sql);
confirmQuery($result);
//set message to user to display
$_SESSION['error'] = false;
$msgs[] = "Client was successfully restored!";
$_SESSION['userMsg'] = $msgs;
}
//display clients page with changes made
redirect('?page=clients');
}
function that displays the message.
//function that shows message and styles accordingly
function message($msgs) {
if(!empty($msgs)) {
foreach ($_SESSION['userMsg'] as $msg) {
if ($_SESSION['error'] == false) {
echo '<div class="noError">'.$msg.'</div>';
} else {
echo '<div class="error">'.$msg.'</div>';
}
}
}
unset($_SESSION['userMsg']);
unset($_SESSION['error']);
echo "hello";
}
here is where i call it in my page....
div class="pageContainer">
<?php
//display messages
message($_SESSION['userMsg']);
?>
</div>
Now when everything is run and i select to edit and client status (or add client, edit client as they all have the same message section, just with different message) i can see the message displayed on the screen. i have a javascript script that will hid the message after 5 seconds, but it doesn't hide. after viewing the page source i notice that the section of code is not visible. again, i can see it clearly see the message that is suppose to be there "Client was set to inactive and removed from the list!" (or whatever message is set to display) but it is NOT show in the view source html.
pic of page loaded with message
here is the view source.....
<div class="pageContainer">
<div>
** updated **
this small example script renders the html, but my above script doesn't...
function setError() {
$error[] = "Password field is to short";
$_SESSION['userMsg'] = $error;
//return $_SESSION['msg'];
}
function message($errors){
if(!empty($errors)) {
foreach ($_SESSION['userMsg'] as $error) {
echo $error.'<br>';
}
}
unset($_SESSION['userMsg']);
}
If I get you right, you want to display $msgs, so why you iterate $_SESSION['userMsg'] ?
Probably the $_SESSION['userMsg'] is null or empty. If you want to display the $msgs just iterate it. And later add to session if really needed. In you code, you always unset the $_SESSION['userMsg'] in the end of function, probably it is always null then. I didn't see the $_SESSION['userMsg'] needed here :
//function that shows message and styles accordingly
function message($msgs) {
if(!empty($msgs)) {
foreach ($msgs as $msg) {
if ($_SESSION['error'] == false) {
echo '<div class="noError">'.$msg.'</div>';
} else {
echo '<div class="error">'.$msg.'</div>';
}
}
}
unset($_SESSION['error']);
echo "hello";
}
UPDATED
After reading your comments, this is related to javascript problem. You should add the id in <div> in order for it's works.
if ($_SESSION['error'] == false) {
echo '<div id="noError" class="noError">'.$msg.'</div>';
} else {
echo '<div id="error" class="error">'.$msg.'</div>';
}

modifying from admin panel and logging out does not save the images

I have a main pain where I want to modify things from the admin account
and whenever I log out of the admin account the images are not saved and it give me an undefined index error.
this is my main page php code:
<?php $src = $_SESSION['firstimgsrc'];echo "<img id='first' src='$src'>";?><!-- Direct link 1st champion -->
<?php $src1 = $_SESSION['secondimgsrc'];echo "<img id='second' src='$src1'>"; ?><!-- Direct link 2nd champion -->
<?php $src2 = $_SESSION['thirdimgsrc'];echo "<img id='third' src='$src2'>";?> <!-- Direct link 3rd champion -->
<?php $src3 = $_SESSION['forthimgsrc'];echo "<img id='forth' style='border-right: 0px;' src='$src3'>"; ?> <!-- Direct link 4th champion -->
this is the admin panel php code:
<?php
if(isset($_POST['submit'])) {
session_start();
$firstimgsrc = $_POST['firstimgsrc'];
if ($firstimgsrc == "") {
$_SESSION['firstimgsrc'] = $jk;
} else {
$_SESSION['firstimgsrc'] = $firstimgsrc;
$jk = $_SESSION['firstimgsrc'];
}
$secondimgsrc = $_POST['secondimgsrc'];
if ($secondimgsrc == "") {
$_SESSION['secondimgsrc'] = $jk1; } else {
$_SESSION['secondimgsrc'] = $secondimgsrc;
$jk1 = $_SESSION['secondimgsrc'];
}
$thirdimgsrc = $_POST['thirdimgsrc'];
if ($thirdimgsrc == "") {
$_SESSION['thirdimgsrc'] = $jk2;
} else {
$_SESSION['thirdimgsrc'] = $thirdimgsrc;
$jk2 = $_SESSION['thirdimgsrc'];
}
$forthimgsrc = $_POST['forthimgsrc'];
if ($forthimgsrc == "") {
$_SESSION['forthimgsrc'] = $jk3;
} else {
$_SESSION['forthimgsrc'] = $forthimgsrc;
$jk3 = $_SESSION['forthimgsrc'];
}
header("Location: ../egamingtv.php");
}
?>
worked, I just used unset instead of the session_destroy in my logout page

php echo wordpress user meta data

On my site when a user is registering there is an option to pick from an additional option that pears their account up with a non-profit. Once the user has registered and viewing specific pages of the site I want the site to be tailored to them using php that grabs their meta info. For this I will echo a button that tailors the front-end based on what meta value they have selected when registering.
If they have no meta key, then nothing is shown.
Here is my code attempt, but does not work!
<?php global $current_user;
get_currentuserinfo(); //wordpress global variable to fetch logged in user info
$userID = $current_user->ID; //logged in user's ID
$havemeta1 = get_user_meta($userID,'nch',true); //stores the value of logged in user's meta data for 'National Coalition for the homeless'
$havemeta2 = get_user_meta($userID,'rotary-international',true); //stores the value of logged in user's meta data for 'Rotary International'
$havemeta3 = get_user_meta($userID,'khan-academy',true); //stores the value of logged in user's meta data for 'Khan Academy'
$havemeta4 = get_user_meta($userID,'wwf',true); //stores the value of logged in user's meta data for 'World Wildlife Fund (WWF)'
$havemeta5 = get_user_meta($userID,'bcrf',true); //stores the value of logged in user's meta data for 'The Breast Cancer Research Foundation'
?>
<!--add if statement to figure out what button to show to logged in user-->
<?php if ($havemeta1) { ?>
<div <p>nch</p> class="Button1"></div>
<?php } elseif ($havemeta2) { ?>
<div <p>rotary-international</p>class="Button2"></div>
<?php } elseif ($havemeta3) { ?>
<div <p>khan-academy</p>class="Button3"></div>
<?php } elseif ($havemeta4) { ?>
<div <p>wwf</p>class="Button4"></div>
<?php } elseif ($havemeta5) { ?>
<div <p>bcrf</p>class="Button5"></div>
<?php } else { ?>
<div><p>None - No Matching Affiliation</p></div>
<?php }?>
-----------------------New Code----------------------
This allows me to see what the affiliation variable is pulling for the user
The result is this: 'User Affiliation: khan-academy'
<?php global $current_user;
get_currentuserinfo();
echo 'User Affiliation: ' . $current_user->affiliation . "\n";
?>
can you pass the meta into a session var, ie $_SESSION['havemeta5'];
get_user_meta is set up wrong. Try this:
$havemeta1 = get_user_meta( $userID, 'nch', true );
...and so on. You need to pass the $userID as the first parameter of get_user_meta() - not $affiliation.
update
If your user meta is 'affiliation' (which allows you to call $current_user->affiliation), you can do something like this:
<?php
global $current_user;
get_currentuserinfo();
$userID = $current_user->ID;
$affiliation = get_user_meta( $userID, 'affiliation', false );
$affiliation = $affiliation[0];
if( 'nch' == $affiliation ){
print '<div class="Button1"><p>nch</p></div>';
} elseif( 'rotary-international' == $affiliation ){
print '<div class="Button2"><p>Rotary International</p></div>';
} elseif( 'khan-academy' == $affiliation ){
print '<div class="Button3"><p>Khan Academy</p></div>';
} elseif( 'wwf' == $affiliation ){
print '<div class="Button4"><p>wwf</p></div>';
} elseif( 'bcrf' == $affiliation ){
print '<div class="Button5"><p>bcrf</p></div>';
} else {
print '<div><p>None - no matching affiliation</p></div>';
print '<div>User ID: '.$userID.', Affiliation: '.$affiliation.'</div>';
}

Drupal login logout links

I've got the following code to show when a user is logged in and when they're not.
<?php
global $user;
if ($user->uid > 0) {
echo "You are logged in as: " . $user->name . "<br>";
} else {
echo "You are not logged in!<br>";
}
?>
I want to add a logout link (when logged in) & and a login link (when not logged in). Would I just use html < a > links? If so, how would I incorporate them?
In most cases you would just use a regular menu and add the two items.
The visibility of the items is controlled by drupals menu-system, so you won't see the login-link if you're already logged in and you won't see the logout-link if you're not logged in.
If you want to output the links programmatically I would recommend using the "l" function
l($text, $path, array $options = array())
To check if a user is logged in you can simply use the user_is_logged_in function provided by the users module.
So your code would become
<?php
if( user_is_logged_in() ) {
print l('logout', 'user/logout');
}
else {
print l('login', 'user/login');
}
Depends on where you want to show the links. If it is on the same page/piece of code, just go ahead and echo them:
<?php
global $user;
if ($user->uid > 0) {
echo "You are logged in as: " . $user->name . "<br>";
echo "Logout";
} else {
echo "You are not logged in!<br>";
echo "Login";
}
?>
You can simply use the url function of Drupal core as below
<?php
global $user;
if ($user->uid > 0) {
echo 'Logout ';
} else {
echo 'Logout ';
}
?>
Hope this will help you :)

Joomla if else for menu by ID or url

I have a situation whereby i want two of my pages to look different form the rest but they are not front pages. If they were it would be easy as the code below would do the trick.
$menu = & JSite::getMenu();
if ($menu->getActive() == $menu->getDefault()) {
echo 'This is the front page';
esle(do something else)
}
?>
In short i want a similar approach but this time, to get the menu by ID/URL. Any ideas?
I got the answer to this...all you need to check is the menu ID then put the code below.
<?php
//This is the code for the page with menu ID 6
$menuID = JSite::getMenu()->getActive()->id ;
if ($menuID == '6')
{
echo '';
}
elseif ($menuID == '2') //This is the HTML for page with menu ID 2
{
echo '';
}
elseif ($menuID == '4') //THis is the html for page with menu id 4
{
echo '';
}
else //This is the HTML code for the rest of the pages
{
echo '';
}
?>
Page ItemId can be obtained using $itemid = JRequest::getInt( 'Itemid' );

Categories