Page 1 of 1

Php & MySQL - User logged in page

Posted: Sat Aug 08, 2009 11:34 am
by onestopphp
Hi experts!
I have just joined this site, hoping to get some help :D .
I have recently just started learning php and I have completed the embedding of a simple user register and login system (using Php & MySQL) to a site.
What I want to ask you guys is how do I redirect a user to their own page. I dont have a problem with redirecting them to a page i've done (eg. loggedin.php) and that already works, even saying "Welcome *users name from the session variable*".
For example, take a big site like facebook. I want each user to go to their own page - a page I may not even have created - am I right in saying the page can be dynamically created if a new user registers?
I think its something to do with redirecting the user via ...logged.php?id ?? :?:

Hopefully my question makes sense and someone will be able to help me!!

Thanks in advance everyone, and I do apologise if someone has already asked a similar question. I've been searching but no luck

Re: Php & MySQL - User logged in page

Posted: Sat Aug 08, 2009 11:51 am
by Aravinthan
Hi,

Ok so what you can do is:

Make a page like a normal page. Then where you want to put the specific details of the user, leave it blank.

So when the page is done, put a restriction to the page so only logged in users can view it.

Then use your session, to take the username, and using MYSQL query, search your database, where usernmae = username for exemple.

Then just output the details on the page.

I hope I was clear, not that good for explaining lol.

Re: Php & MySQL - User logged in page

Posted: Sat Aug 08, 2009 1:28 pm
by aceconcepts
Yes, when displaying user-specific content, this content is deemed dynamic. It's dynamic because it is based upon the a single foundation or structure. However, If I logged in using a unique identifier e.g. 123 my content will be my own and will differ to content related to id 124 (by use of a relational database).

Like Aravinthan mentioned, you will have a page like myprofile.php as the basis of all users content. Within this file you will have the ability to query your database for user-specific records. This is usually done by using url parameters e.g. myprofile.php?id=123&show=contacts.

This should help lead you in the right direction. It's all about getting the logic correct :D

Re: Php & MySQL - User logged in page

Posted: Sat Aug 08, 2009 6:04 pm
by onestopphp
Hi thank you guys for quick replies!

I sort of understand what you mean. Im sure I am kind of thinking along those lines, but I dont know how to put that into actual code.
Does any one mind showing me a quick example?

edit: I understand what you guys are saying, I do have a page, loggedin.php, which shows when the user is logged in succesfully. I can even show their details,such as name frm the database / session['$username'].. What i dont get is, when can you use this 'loggedin.php?id=' method? whats that used for then?

In the meantime i'll try and do something too!

cheers :D :mrgreen:

Re: Php & MySQL - User logged in page

Posted: Sat Aug 08, 2009 7:00 pm
by peterjwest
Here's a really trivial example using an array instead of a database. You could easily store and retrieve this array from a database:

Code: Select all

 
$userPages = array(
   'john'=>array('email'=>'john@blah.com', 'bio'=>"I'm a rocket scientist from Ipswitch"),
   'paul'=>array('email'=>'paul@blah.com', 'bio'=>"I'm a brain surgeon from Sheffield"),
   'moron'=>array('email'=>'moron@blah.com', 'bio'=>"I'm a moron from London"));
if(isset($_GET['page']) && isset($userPages[$_GET['page']]) {
   $userPage = $userPages[$_GET['page']];
   echo '<a href="'.__FILE__.'?page='.$_GET['page'].'"><h1>'.$_GET['page'].'</h1></a>';
   echo '<p>'.$userPage['bio'].'</p>';
   echo '<a href="mailto:'.$userPage['email'].'">'.$userPage['email'].'</a>';
 
 
 
You can access, for example, john's page at: filename.php?page=john
There are other (prettier url) ways to do it, but this is the simplest.

Re: Php & MySQL - User logged in page

Posted: Sat Aug 08, 2009 7:44 pm
by aceconcepts
Hey, what's with the dig at Londoners eh? :x

Re: Php & MySQL - User logged in page

Posted: Sun Aug 09, 2009 7:18 am
by onestopphp
@peterjwest - cheers but I dont want to do it via an array!
I can manage to show the users's details, name, email etc.. all that works fine.
What is this method actually called? - page.php?id= blah blah.. and when is it actually used?

I'll do some research on it when i know its name ey

@aceconcepts - what dig? :?

thanks for the help so far

Re: Php & MySQL - User logged in page

Posted: Sun Aug 09, 2009 8:22 am
by peterjwest
My example Londoner was a moron :P (I'm from London).
onestopphp wrote: What is this method actually called? - page.php?id= blah blah.. and when is it actually used?
I don't know the technical name but it's essentially setting variables in the URL and retrieving the values using $_GET.

For example if there is a webpage 'index.php' and I access this webpage using: 'index.php?var1=monkey&var2=banana'
Then the script in index.php will have access to $_GET['var1'] => 'monkey' and $_GET['var2'] => 'banana'.

If you look around the web you'll see $_GET variables are used extensively (they should be visible in the URL of this thread). The main advantage is it allows a single webpage to perform multiple actions, which you can create hyperlinks to.

Another example: 'index.php?view=html' and 'index.php?view=rss' might switch between HTML page and an RSS feed.

Notice in my example I created a title for each page which also creates a link to that page. You can create a list of all these links on your homepage.