0

I want to know how I would get user 'McKenzie' to see his own unique page that he can manipulate and 'Wendy' to see her own page when she logs in.

I've created the login and the pages, connected them to a MySQL database given them ID's etc, so I can do all of this and I know about sessions etc. ;)

So can someone tell me how I would do this, would I have to make different pages for each separate user? I'm thinking something along the lines of

REGISTER PAGE:

Store data in database, get user ID and use "?pageid=1" to then take the user to the id based page.

But I'm not sure how I would make each page without making them manually, as you can imagine making a new page for each separate user would be a pain... and very inefficient. Any ideas?

And please show me an example with code, it would be GREATLY appreciated! Thank you all in advance!

The Dog Mic
  • 7
  • 1
  • 6
  • You might want to look up information about session ids. You are asking a lot and it may take several scripts to do what you need. I recommend reading a few tutorials before asking questions like this in SO. Here's an example one: http://www.w3schools.com/php/php_sessions.asp – nomistic Mar 28 '15 at 23:23
  • i know about sessions i know how to use start end them etc, i just dont see how i would use them to create a new page, a session cant make a new page? it only stores a new variable, but thanks for the link – The Dog Mic Mar 28 '15 at 23:28
  • You wouldn't use them to create a new page. You would do that with php, and then use the session id to identify the logged in user. You will need a script that creates a user, with a password (I hope you would encrypt this), then a page which enables login, and then a page, that allows logged in users to add something, and a page which shows all the items that they have added, etc – nomistic Mar 28 '15 at 23:31
  • please show your attempts – nomistic Mar 28 '15 at 23:32
  • Ive put in the question all of that, ive done all of that. All i am asking is 1 thing how would i make a different user see a different page without physically creating the page ive made the login script and register script and mysql encrypted database. – The Dog Mic Mar 28 '15 at 23:34
  • If you have a variable like pageid, you can get that using `$_GET['pageid'] && $_GET['user_id']` assuming you have used that as part of your session variables. – nomistic Mar 28 '15 at 23:35
  • I mean `$_SESSION['user_id']`. Note you will need to create a separate php page to include the variables, but it would be the same for every user, just with different data based on you sql query and their user_id. – nomistic Mar 28 '15 at 23:35
  • Okay lets start again, ive made a login page, ive made a register page, ive used sessions to encrypt a login page. the problem is each user sees the same page unless i create a new page manually and redirect. I want user mckenzie to see "mckenzie's page" Without having to manually create this myself. – The Dog Mic Mar 28 '15 at 23:38
  • can you show me an example please, if you dont mind. – The Dog Mic Mar 28 '15 at 23:39
  • see possible answer below – nomistic Mar 28 '15 at 23:40

2 Answers2

0

Here's an example of what you could do:

<?php

  if (!isset($_SESSION['user_id'])) && (!isset($_SESSION['user_name'])) {
    echo '<p class="login"><a href="login.php">log in</a></p>';
    //exit();
  }
  else {
$user_name = $_SESSION['user_name'];

    echo('<p class="login"><a href="mypage.php">' . $user_name .'\'s page</a> | <a href="logout.php">Log out</a></p>');
  }

?>

There's a lot more you could add, but this is just to generate information on whether they were logged in.. If the $_SESSION['user_id'] is set, you can then generate code based on that information. (note, you would need to create the $user_name or whatever variable, likely from an sql query)

nomistic
  • 2,902
  • 4
  • 20
  • 36
  • Wouldnt it return a notice/ error since the page mypage.php 'user_name' does not exist? – The Dog Mic Mar 28 '15 at 23:44
  • well of course. I'll edit this to make it a little more clear. take a look now – nomistic Mar 28 '15 at 23:47
  • note, this assumes `user_name` is also stored as a session variable. If not you could just have a query from your database from `user_id`, and bring that in. – nomistic Mar 28 '15 at 23:51
  • Okay thats cleared one thing up for me but now how would i create this page without having to do so manually? is there not an: if mypage.php/mckenzie does not exist then create it and include homepage.tmpl.php do you get what im saying? – The Dog Mic Mar 28 '15 at 23:58
  • yes. the above code would need to be entered onto a new mypage.php that you have created. I don't know what your homepage.tmpl.php has; and as there is likely to be some content that will be different for logged in users, you'd need to create that. As long as you have user id on your tables and this is linked to the rest of your tables (if you haven't already done so, you will have to modify them to include a foreign key to your user table), you can generate custom content for each user. However, no, you would not need to create a separate page for each user :) – nomistic Mar 29 '15 at 00:04
  • Note, you could always do all of this in one script, if you want to have a lot of conditional data in the main script and have your forms self-reference, and not move between pages, but my personal taste is to have more scripts with less info; they are easier to understand and debug if necessary. However you can have both if you like using includes as you suggest :) – nomistic Mar 29 '15 at 00:08
0

My answer is assuming you want to create fully customizable user data with the added possibility of sharing the page between users (like a profile page). With that out of the way you can do this by creating one php page that searches the MySQL table by $_GET or $_POST data.

Ill expand this answer in to a couple of steps...

SQL Tables

The first thing you will need is your MySQL set-up, ill assume you have a basic set-up already done but I will go ahead and create a simple one.

The basic set-up will be the login data and the custom user data, you can view my set-up here.

php user page

The simplest way would be to get the requested user from the $_GET data. So to do this we would simply get the data and request the users information:

$requested_user = $_GET['id'];

$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'root', 'MyPassword');

try {

    $stmt = $db->prepare("SELECT * FROM c_userpage WHERE id = ?");

    $stmt->execute(array($requested_user));

    $mydata = $stmt->fetch();

} catch (Exception $e) {

    //error with mysql
    die();

}

Now we can simply add the users data to the page!

echo "Hello! my name is {$mydata['username']}!\n";
echo "About Me: {$mydata['custom_data']}";

Sending users to their page

We can simply just use www.page.com/user.php?id=2 And this will request the data for the user with id=2

Extras

If you want to keep user pages private you can simply request the id with $_POST or $_SESSION and then check if the user is logged in!

Full code for user.php

Full code for user.php w/ private page

MorganF
  • 73
  • 6
  • Great! If you need any info added just ask! – MorganF Mar 29 '15 at 00:56
  • nicely explained. However there are some security holes, specifically in storing the user_id as a `$_GET` variable. Depending on the purpose of this site, it would make it relatively (read: very) easy to access another user's personal page. – nomistic Mar 29 '15 at 01:02
  • True, but as I said above this was built around sharing a users "personal" or customizable page. In the end I added the code for a private page that only the user can see. – MorganF Mar 29 '15 at 01:06
  • 1
    Understood. However using `www.page.com/user.php?id=2` makes it very easy for an already logged in user, to simply change the id to something else to start browsing around someone else's page. There's a simple solution; simply store id in the login page as a session id, and then grab that using `$_SESSION['id']` instead. (I learned this the hard way myself) – nomistic Mar 29 '15 at 01:12
  • I agree, when I originally started the question I was unsure if the poster was referring to a private user page or a public profile page customizable by the user (That is shareable via link ), so I added both. Perhaps I will edit my answer to highlight the differences a little more. – MorganF Mar 29 '15 at 01:19