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
Adam Duffield
30,494 PointsHow to add user recognition to a site in php?
Hi All!
I'm getting pretty familiar with PHP, SQL and all the general languages needed to build any type of site from scratch.
How would i go about build a miniture facebook like website? I'm not trying to re-create facebook i just want to be able to show off some basic functionality add a friend, a picture and posts so that when your friend logs in they can see your posts or others posts.
I've cracked the register and login part and coded it into a database and I feel confident in the post part too but my main concern is once logged in, how do i get php to recognise your session? e.g.
If i login as Jim or Bob this mock site still brings me to homepage.php it doesn't recognise a specific user.
I could also probably do with a hand covering adding friendships too but pictures i can probably do myself :)
It's a shame Treehouse don't have a course on this kind of stuff in PHP from what i read it's a very popular language!
Many Thanks,
Adam
2 Answers
chrisp
13,686 PointsPretty cool project. I am not sure how you set up your database and your database is design. Here is something I would do starting with a relative database. Here's a small of how would I do it.
1) member table with ID 2) entries table
Get all your databases fields figure out and implemented. Once those are design I would start with PHP store a unique memberID in the session for every users. Here is basic session output of how I would do it.
http://www.w3schools.com/php/php_sessions.asp
//includes your config and connections.
............
//Open a session
session_start();
//get member data, go through the validation.
..............
// generate a session ID
session_regenerate_id();
//fetch your members data.
.............
// Store specific data as session.
$_SESSION{ ' give a session name'] = $something['member/username/etc]'
//When all session are stored, close it.
session_write_close();
This would allow to remember data in session for users and allow only contents that belong to the user. This is a small to your project, but I hope this helps a little as a start. Good luck.
Louis Otto
23,264 PointsHi Adam, it's possible to do quite easily in PHP :)
Here are the objectives and their solutions
1) How does the site know if a person has just logged in?
If they logged in using an email address or username, that is the identifier by which the site can know 'who' they are.
2) I should store this information so the browser can use it.
Use a cookie!
// Capture email address from form submission
$_POST['email'] = $emailaddress;
// Set cookie with value
setcookie('email', $emailaddress);
Now on the home screen (after logging in) you can set a database request to find the users record with that email. Capture the name elements into an array and simply finish off with:
// Make your database call in whichever method you use to capture the name into a variable like $name
// Set cookie with name value for future reference
setcookie('name', $name);
// Read back value from cookie
echo "Welcome: " . $_COOKIE['name'] . "!";
The benefit of using a cookies for this sort of information is that information can be stored so when they log back in you can get clever with your code so you don't need to make a database call each time by checking if a cookie exists :)
Hope this helps you out