Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Fabio Neto da Silva
12,440 PointsPhp user id
Hello,
i am trying to build a simple social network with php.
But i have a big question,
what's the best way to pass the user id throw every page?
I am working with session start(); Can i use my session start to do this job and is it secure?
thanks
5 Answers

Carles Jove i Buxeda
4,281 PointsHello Fabio,
The basic usage of PHP Sessions is explained here on the docs. Basically, what you need to do is:
- Validating the login info against the database
- Starting a new session (if it's not started yet)
- Passing the current user information to the
$_SESSION
array
Assuming you have a users
table in the database, with name, email and password fields, a very simple implementation would be:
// Get login data
$email = $_POST['email'];
$password = $_POST['password'];
/* Validate User */
function auth_user( $email = '', $password = '' ) {
// Query Database for an $email & $password match
$current_user = "SELECT * FROM users WHERE 'email' = $email AND 'password' = $password";
if ( $current_user ) {
return $current_user;
}
return false;
}
if ( auth_user( $email, $password ) ) {
// If the session wasn't started, start it now. PHP >=5.4.0
if ( session_status() !== PHP_SESSION_ACTIVE ) {
session_start();
}
$_SESSION['current_user'] = auth_user();
} else {
// Do whatever here
}
// At this point, $_SESSION['current_user'] is a valid user
$current_user = $_SESSION['current_user'];
echo $current_user['name']; // Outputs the current user name ;-)
Of course, this is just an example of the basics, but you should take more steps to validate data, encrypt passwords, and probably wrap things up in classes. I'd strongly reccommend that you use a framework such as CakePHP or FuelPHP, because coding from scratch is not always the best idea. If for whatever reason you do not want to use a framework, then I'd suggest to checkf for an Auth library and use it. That will help you make your app safer and less prone to errors.
Hope it helps.
PS 1. By the way, there use to be a PHP course by Jim Hoskins where he taught how to authenticate a user. I remember it because it was my very first experience with that. I don't know if that is still available in the Treehouse archives ;-)
PS 2. Unless you must do it in PHP, there is a really great course here in Treehouse for creating a social network with Ruby on Rails. I guess most of your questions and problems will be treated there, and it could be a perfect chance to learn Ruby. Since I took the course I'm no longer using PHP for complex apps, but Rails.

Fabio Neto da Silva
12,440 Pointshallo Carles Jove i Buxeda
Thank you for your explanation. i have learn some things.
not long time ago, i started learning ruby on rails here on treehouse, I found it easier to learn; but because i don't wanted to learn two programming language together, found it confusing, I've stopped with ruby on rails, because I've been working for a while with php.
I really don't know what to do, i found ruby, safer, faster, funnier to learn, but i already lost a lot of time with php to give up now. And i am afraid of hosting issues for ruby on rails, beacuse i can't find any host company in belgium with ruby on rails support.
What should i do? i really dont know? Does ruby on rails have more future than php?
Thanks

Carles Jove i Buxeda
4,281 PointsHello again, Fabio.
I can't tell you what to do, but here there go some advices based on my own experience:
Programming is hard and difficult. You better learn one language well, cause that expertise will help you learn others faster. If you choose PHP, stick to it.
No programming language is better that another, but sometimes one can serve your interests at hand better than another. You can build the same web apps with PHP or Ruby, so choose the language according to your knowledges, available tools, etc.
Rails is amazing, but unlike PHP, deployment can be really hard. No such thing as FTPing the files to the server. Local development may seem easy, but thing can get pretty complicated after a while. In that sense, PHP apps might be easier to manage. Choose wich language or framework to use according to the project needs.
Rails hosting is more expensive and less common. That's a fact. You must know it beforehand.
As a begginer, it's very common to build everything yourself. After more than 5 years doing so, I've found it's the most stupid thing to do. There are frameworks that solve the most common situacions, and help you write cleaner and safer code. Also, most of them have built-in tools for writting tests, which is very important. Get into MVC frameworks such as CakePHP or FuelPHP. You may need a few days to learn, but trust me, it's worthy.
As a developer, you must know when to say no to a project. If you're just starting with PHP, maybe building a social network for a client is not a good idea. It's hard to say no, but you rather say "I can't do it" than attempt to do something you're not prepared for. Both your client and you will be happier at the end, and be sure you'll get some other projects you can work on.
I know this goes beyond the "PHP user id" question you made, but I hope it will help. My experience is that sometimes the best thing to offer to a developer is not a piece of code, but a piece of one self's experience ;-)
PS. By the way, do you know Ning? Maybe it could help you build this project, if you decide to take it.

Fabio Neto da Silva
12,440 Pointshallo, yes, i think i will stick to php, i would love to work with some php framework, but treehouse doesn't provide any courses with php frameworks, where can i find some good tutorials.
Which framework should i learn? what is the easiest? cakePHP , Fuelphp or lavarel?
My social network is only for learning purposes, it has several functionalities, and I thought i would learn faster this way.
Btw thanks for helping me

Carles Jove i Buxeda
4,281 PointsCakePHP is great, and is well documented, which is very important. Also, even though is generally an Object Oriented framework, the data returned from the database are arrays, which might make it a little easier to deal with if you're starting with OO programming. It has some Auth functionalities out of the box, also. So yeah, that's a good choice ;-)
Cheers!