Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Need some PHP help

Featured Replies

Hello my fellow WDF members,

 

If you haven't already seen my topic for me making my own forum script from scratch you can view it here: http://www.webdesignerforum.co.uk/topic/44934-my-own-forum-script/page__p__282167#entry282167

 

Basically the help that I need links to the code for the script. So here it goes.

 

I want to make a link appear only to an admin. Admin being user level 1.

So I'm guessing it has something to do with,

$_SESSION['user_level'] != 1 

. So how can I do make it so that a link only appears for a user of user level 1 only!!

 

Not sure if that makes too much sense. If not please tell me :/

 

Danny.

That is fine, you just need to use an if statement around it and just check that it equals 1

<?php if($_SESSION['user_level'] == 1) : ?>
<a href="/blah">link</a>
<?php endif; ?>

  • Author

EDIT: Non-Admin members can't see the link. However guests can! :(

Edited by DannyG17

If that's the case you've got the wrong session vars - echo the session at the top of the code so you know what it is

<?php
echo '<pre>'.print_r($_SESSION, true).'</pre>';
?>

And post back what the value is if it's something strange

  • Author

That worked, but the code is visible on the template.

(
   [signed_in] => 
   [user_id] => 
   [user_name] => 
   [user_level] => 0
)

EDIT: Non-Admin members can't see the link. However guests can! :(

Personally i'd set permissions in the database not in a session :) set up a field called admin with 2 possibly values 0 being non admin 1 being admin then u can check permissions like this, using a simple user created function

 

function is_admin($session){
$sql = mysql_query("SELECT * FROM table WHERE id = '$session'");
$row = mysql_fetch_array($sql);
if($row['admin'] == "1"){
return true;
}else{
return false;
}
}

 

Then you can check it like this

 

if( is_admin($_SESSION['user_id'])){
//Show only what an admin should see
}

  • Author

 

 

function is_admin($session){
$sql = mysql_query("SELECT * FROM table WHERE id = '$session'");
$row = mysql_fetch_array($sql);
if($row['admin'] == "1"){
return true;
}else{
return false;
}
}

 

 

 

where does that bit of code go?

 

I'd like to point out I have never used PHP. I have learnt a lot already just from looking at code today. Hence why I needed a PHP Coder :p

 

But I still want to learn PHP so this project is great for me :)

Edited by DannyG17

That worked, but the code is visible on the template.

(
   [signed_in] => 
   [user_id] => 
   [user_name] => 
   [user_level] => 0
)

 

OK well that code doesn't actually do anything other than show you the current users session, it was used just to see what the values in $_SESSION were. By the sounds of it you hadn't set the user level to 0 on log in/out properly for the guest. You can remove that bit of code now

where does that bit of code go?

 

I'd like to point out I have never used PHP. I have learnt a lot already just from looking at code today. Hence why I needed a PHP Coder :p

 

But I still want to learn PHP so this project is great for me :)

 

This is just a simple function to check if a user is an admin or not, u can put it in a global function's file for ur script or where ever

  • Author

This is just a simple function to check if a user is an admin or not, u can put it in a global function's file for ur script or where ever

 

Ha I'm still lost?

To be honest you shouldn't be starting on making a forum if you're this new to PHP. It's pretty crazy IMO. Start small and build on your experience

  • Author

To be honest you shouldn't be starting on making a forum if you're this new to PHP. It's pretty crazy IMO. Start small and build on your experience

 

I like doing it this way. Trust me It's the fastest way I learn. Is how I learnt HTML and CSS.

Is how I learnt HTML and CSS.

 

HTML & CSS is not programming. PHP is debatably getting into real programming. There is a significant difference in complexity and, unlike HTML/CSS, it's crucial you don't do a dodgy job.

 

I seriously recommend starting something smaller than a forum. If you're under the impression that if($_SESSION['admin'] == 1) is a safe, secure way to check for an admin - you've got a lot to learn!

 

  • Learn the basics at W3Schools
  • Learn a bit about data structures and the like via the Interwebs
  • Learn about different OOP Concepts; inheritance, abstraction, polymorphism, etc.
  • Learn about OOP in a general sense
  • Get straight into OOP with PHP
  • By this point you should have a reasonable understanding - the learning of syntax/built-in methods will come with time

 

I'm hoping someone like Jock will visit the thread and give you a good-and-proper list for you, but the above should get you to the beginner stage of PHP development.

Edited by andyl

Ha I'm still lost?

Mmmm im confused? do u not even know what a function is? cause i seen ur forum and unless u copied the code to the tee which i think u sorta did cause i seen that tutorial before and its the exact same style. The u should step back like jay said and learn this stuff first, gotta learn to walk before u can run :)

  • Author

Mmmm im confused? do u not even know what a function is? cause i seen ur forum and unless u copied the code to the tee which i think u sorta did cause i seen that tutorial before and its the exact same style. The u should step back like jay said and learn this stuff first, gotta learn to walk before u can run :)

 

Read my other topic (link at top)

 

I have already learnt some php by looking, and have designed a simple Admin CP.

Read my other topic (link at top)

 

I have already learnt some php by looking, and have designed a simple Admin CP.

I did read it and looked at v0.1 of your forum and it looks like you copied the style ect.. from the tutorial you followed. And like Andy said doing if($_SESSION['admin'] == '1') is not secured cause sessions can be hijacked very easy

  • Author

I did read it and looked at v0.1 of your forum and it looks like you copied the style ect.. from the tutorial you followed. And like Andy said doing if($_SESSION['admin'] == '1') is not secured cause sessions can be hijacked very easy

 

Woops, thought I put on there that I used the source files.

  • Author

Mmmmm then how can that be u making a forum? its someone else making a forum for u, with u just adding bits and bobs

 

That's what I was trying to get at. Wanting to expand from where he left off.

I feel like my post has been overlooked Danny?

You seriously need to grasp even the basic programming concepts before you get yourself into terribly insecure, bad habits.

 

Don't just jump in the deep-end with this, it won't work. HTML & CSS is child's play if you've got the patience. PHP is a few steps ahead.

I feel like my post has been overlooked Danny?

You seriously need to grasp even the basic programming concepts before you get yourself into terribly insecure, bad habits.

 

Don't just jump in the deep-end with this, it won't work. HTML & CSS is child's play if you've got the patience. PHP is a few steps ahead.

Sorry i deff did not overlook ur post u pretty much gave it in a nutshell lol very good advice for danny or whoever to be honest, sometimes beginners dont like to here it though +1

Sorry i deff did not overlook ur post u pretty much gave it in a nutshell lol very good advice for danny or whoever to be honest, sometimes beginners dont like to here it though +1

You don't need to apologise - you're not the guy I'm worried about here ;)

Edited by andyl

Actually HTML/CSS is not programming at all and alot of designers really hate me when i say this but its a hardened truth and could be a painful one if you jump into the deep end without as much as even knowing half the functions that are happening within the forum system. And seeing that you pretty much don't know what a function is it really shows that you need to open up the PHP manual and study or like andy mentioned w3schools.

Edited by webdesigner93

  • Author

I didn't overlook your post. I'm on W3Schools now and I know HTML and CSS isn't programming.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.