2

I have set up a php script that will redirect users to a particular webpage/ php page after logging in. But I want to direct each user to a different page.

For example each customer that logs on has their own and unique homepage.

If have this section in my checklogin.php and I am assuming it is something to do with this. I have only recently started learning php. Thank you.

<?php
    session_start();
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    mysql_connect("localhost", "root","root") or die(mysql_error()); //Connect to server
    mysql_select_db("first_db") or die("Cannot connect to database"); //Connect to database
    $query = mysql_query("SELECT * from users WHERE username='$username'"); //Query the users table if there are matching rows equal to $username
    $exists = mysql_num_rows($query); //Checks if username exists
    $table_users = "";
    $table_password = "";
    if($exists > 0) //IF there are no returning rows or no existing username
    {
        while($row = mysql_fetch_assoc($query)) //display all rows from query
        {
            $table_users = $row['username']; // the first username row is passed on to $table_users, and so on until the query is finished
            $table_password = $row['password']; // the first password row is passed on to $table_users, and so on until the query is finished
        }
        if(($username == $table_users) && ($password == $table_password)) // checks if there are any matching fields
        {
                if($password == $table_password)
                {
                    $_SESSION['user'] = $username; //set the username in a session. This serves as a global variable
                    header("location: home.php"); // redirects the user to the authenticated home page
                }

        }
        else
        {
            Print '<script>alert("Incorrect Password!");</script>'; //Prompts the user
            Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
        }
    }
    else
    {
        Print '<script>alert("Incorrect Username!");</script>'; //Prompts the user
        Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
    }
?>

enter image description here

Rus84
  • 33
  • 2
  • 11

4 Answers4

1

You can add a column on your database: "level" and doing some IF's you can redirect them to the page that you want.

EDIT:

Edit your code where you have:

 if($password == $table_password)
                {
                    $_SESSION['user'] = $username; //set the username in a session. This serves as a global variable
                    header("location: home.php"); // redirects the user to the authenticated home page
                }

Change to:

 if($password == $table_password)
                {
                    $_SESSION['user'] = $username; //set the username in a session. This serves as a global variable
                    header("location: home.php?user=".$username); // redirects the user to the authenticated home page
                }

And on home.php write:

<?php

$username = $_GET['user'];

echo "I'm on page home with user:" , $username;

?>

Now you can do whatever you want, you have an dynamic webpage, you can "play" with database create new columns and query on home.php using WHERE username=$username

mmarques
  • 296
  • 1
  • 9
  • I currently using MAMP and have the user details stored in a database. At the moment the only login details are a user name and password. What do you mean by 'level'? – Rus84 Mar 17 '15 at 11:55
  • By using level , i mean share users by sections. And you can check if section of user = "customer1" then redirect him to database1.php elseif section of user = "customer2" .... – mmarques Mar 17 '15 at 11:59
  • aah that makes sense, thanks, but i am trying to work out how to redirect them. At the moment, once the password and username has been verified they all get redirected to the home.php page which takes them to one particular webpage, I have 4 separate php files, and i need my 4 users to be directed to their own unique page. Thanks. – Rus84 Mar 17 '15 at 12:26
  • Oh.. i know what you want.. Dynamic Web pages i think. You should have one php only like "user.php" check out my pastebin: http://pastebin.com/bhFRsGZV , you will notice the title when you redirect will have your username. – mmarques Mar 17 '15 at 12:31
  • After looking at a few forums i think you are right about having a dynamic web page. I have amended my question at the top of this page and have inserted a checklogin.php. At the moment i do not have a user.php I believe that I have incorporated some of it in the checklogin.php. Or do i need to also create a user.php? Because i will need somewhere to tell the program where to send each user? – Rus84 Mar 17 '15 at 13:06
  • If all the information is in another table, he probably linked that with the ID since that's his primary key. – Loko Mar 17 '15 at 13:29
  • @Rus84 Check my post (EDIT). – mmarques Mar 17 '15 at 13:29
  • @Loko ye we can redirect by ID aswell , i upvoted your answer .. and since ID is primary key , you're right :) – mmarques Mar 17 '15 at 13:30
1

I assume you have information about them in the database that you want to show.

Now you can just direct them to the url with a unique ID of the account(that should also be stored in the database) e.g: index.php?id=$account_id,

Get the ID from the url(somewhat like: $_GET['id']) and run a query e.g: select * from profile where id=$id

NOTE{

I am using the variable $id here instead of $_GET['id'] because of the possible mysql injection.

}

After that you show the accounts profile information on the page and because of the &id=$account_id(where $account_id is the ID of the person logged in), it's their own page.

EDIT:

To redirect them to their own page you have to get the ID from the database with a query:

select id from accounts where username = $username;

This works if the username is unique.

You can get the id in a seperate variable yourself.

After that you can use the ID to direct them to their own page like this:

header("location: home.php?id=".$id);

EDIT2:(Based on the comments)

    while($row = mysql_fetch_assoc($query)) //display all rows from query
    {
        $table_users = $row['username']; // the first username row is passed on to $table_users, and so on until the query is finished
        $table_password = $row['password']; // the first password row is passed on to $table_users, and so on until the query is finished
        $table_id = $row['id'];

    }
    if(($username == $table_users) && ($password == $table_password)) // checks if there are any matching fields
    {
            if($password == $table_password)
            {
                $_SESSION['user'] = $username; //set the username in a session. This serves as a global variable
                header("location: home.php?id=".$table_id); // redirects the user to the authenticated home page
            }

    }

This will redirect you to home.php everytime. The only difference is, the ?id= part is different for every user. Now on home.php:"

$id=$_GET['id'];
//Some mysql injection prevention.
$query = mysql_query("SELECT * from profile WHERE id='$id'");

This means you should store the profile preferences of every user in the database and get it out again at the home page.

Community
  • 1
  • 1
Loko
  • 6,539
  • 14
  • 50
  • 78
  • Thanks for that, most makes sense (still learning how to use PHP and how to piece everything together for MAMP). Each user has a table of figures that is only to be seen by their company. Each table is stored in a separate php file. Therefore when they login in i want them to be directed to their own php file. I have the query set up that checks that username and password, but at the moment all users are directed to the home.php file. Do i then redirect users in the home.php file? Can you give me an example of how to redirect them, the code. Many thanks. – Rus84 Mar 17 '15 at 12:24
  • @Rus84 Sure but first, do you have a primary key in your database(I assume you have) if so, what is the primari key? User_ID or Username?? – Loko Mar 17 '15 at 12:30
  • The primary key is the ID (the other two columns are username and password) – Rus84 Mar 17 '15 at 12:42
  • Thanks for the ammendment. I have changed my question at the top and have added in the checklogin.php, this obviously checks that my username and password matches the ones that are stored in the database. Do i then need to add the select from id line as well? Really stupid question..... at what point do i match the user to the page i want them to be redirected to? Do i have to write that bit in the home.php? cheers for your help – Rus84 Mar 17 '15 at 13:24
  • @Rus84 You already have this line: `$query = mysql_query("SELECT * from users WHERE username='$username'");` So now you get the `row['id']` out of that and use that in the `header("location: home.php?id=".$id);`. You dont have to do anything else. You can just use the `$_GET['id']` on the next page and use that in another query. – Loko Mar 17 '15 at 13:31
  • Hi Loko, I am just revisiting this project as another task has had to take priority. I understand the principles of what you are saying, but slightly confused about how to achieve them. In the Checked.login.php do i need to add a line under $table_users, and write &table_id??? When you mentioned about just using $_GET['id'] on the next page, do i need to write another php script? or does that line slot into the checked.login.php page? If i got 3 different users and want to redirect direct them to 3 different file names, would all of that be included in this script? Thanks – Rus84 Apr 13 '15 at 16:02
  • @Rus84 Hello, So I have to get in this question first as well. Sorry what do you mean with a line under $table_users and write &table_id? Also the thing about every user being redirected to their own personal page, is that they actually all get sent to the same page except you give their ID's to that page. So you get different information. `$_GET['id']` Should be on the page that's going to be their personal page. This is the code to send them to the other page with their own id's: `header("location: home.php?id=".$id);` – Loko Apr 14 '15 at 07:12
  • Sorry that was bit of a typo.I have attached an image of the user details,ID is primary key, username and password. Is the ID column sufficient or do I need another column where i create individual customer ID numbers? So on the home.php I should include $_GET['id'] ?? Is that correct? Just to reword what you have writtent to make sure that i understand it..When each user logs in, they will all be sent to the same page (e.g.home.php). This page will then get different information based on their ID.Do i need to create a variable linking the location i will send the customer and their ID? Thanks – Rus84 Apr 14 '15 at 09:27
  • @Rus84 Perfect. The ID in your table is what you want. So you get the users ID from the table and put that in the `?id=`. The rest of your comment is exactly what I meant. Al though the page doesn't **automatically** get different information of course. You need to run a query with the `$_GET['id']` on the home.php . Then you get the information you want of the user out of whatever table you use to personalize the home.php. – Loko Apr 14 '15 at 09:36
  • @Rus84 Let me know if it worked. If it did, accept an answer. Which ever one is the most usefull – Loko Apr 14 '15 at 10:06
  • That seems to be sense (apologies, it wasn't that long ago that i started to learn all of this), where would i stores the information about where to direct each customer for example ID 1 go to London.php, ID2 America.php ..... IS that stored in the database or is that all written in the home.php. I.e. they get sent to the home.php, and once they are there, their ID is checked and then redirected to the correct place? Cheers – Rus84 Apr 14 '15 at 15:04
  • ire your examples in the opening parts of this forum still valid? But i would change username to ID as the ID is the primary key? – Rus84 Apr 14 '15 at 15:11
  • @Rus84 It should all just be in home.php home.php?id=1 and home.php?id=2 will have different information. It just stays on home.php. On the home.php you just run a query with the id that's in the url and then show the result on the page. So home.php?id=1 and home.php?id=2 are both different. – Loko Apr 14 '15 at 19:18
  • ok cool, but what do you mean by home.php?id.1 ??? I want to direct the user to a php page not a url. Is it easier to name the php script the same as the username? The bit I'm trying to get my head around is where do i store the information about where that customer is to be directed. The home page gives them instructions but where and how to you write the bit....... user 1 is to be sent to 1.php, user 2 to be sent to 2.php thanks again – Rus84 Apr 15 '15 at 12:31
  • @Rus84 home.php?id=1 is just basically home.php except you give some extra information to the page. With that extra information you can get different information on the same page. – Loko Apr 15 '15 at 12:32
  • aah so the end part is the detailed section that will direct each person to their unique page? But where is the information stored about where i send them. Your home.php?id=1 is obviously for ID1 but based on this how does it know where to be redirected? Where is this vital information stored? thanks – Rus84 Apr 16 '15 at 09:40
  • can you possible write part of the get query for the homepage, I have tried to write php on the homepage but i am not being redirected, I am able to get text to appear but not to redirect to the correct php page. thanks – Rus84 Apr 17 '15 at 12:29
  • @rus84 It doesn't know where to redirect to **since you're being redirected to the home.php** with a different ID depending on the users id. I'll edit my answer. – Loko Apr 17 '15 at 13:05
  • That is great, thanks, sorry for being repetitive, I understand that everyone is directed to the home.php. Then a get query is needed to get the relevant information to be shown on that page. For ID1, America.php will be shown. Is this information stored in the database? or Do i need to write a few lines in the home.php? – Rus84 Apr 17 '15 at 14:14
  • Sorry, i have just seen the line underneath your edit, i think that is the answer i was looking for. cheers – Rus84 Apr 17 '15 at 14:20
  • Sorry, i have just seen the line underneath your edit, i think that is the answer i was looking for. I am trying to a row called 'page' and this will contain the information about where each user should be sent i.e. which page they should go to. does that sound correct? cheers – Rus84 Apr 17 '15 at 14:59
0

If you want to dynamically redirect the user to another webpage, then use javascript. PHP "only" send content of a specific file. You could also just use the same page for all users, but change it's content. The most trivial case I can think of, is remove all content of - say - user.php and load the html from a database, or another file according to the user.

rst
  • 2,510
  • 4
  • 21
  • 47
  • Thanks for your reply. Each user has a page that contains a unique database for their company. I am using MAMP and the login details for the customer are 'username' and 'password'. I want to redirect customers in the following way customer1 to database1.php, customer2 to database2.php. – Rus84 Mar 17 '15 at 11:57
0

I think you can redirect users to the same page (ThisUserProfile or smth like that), but generate content for this page in dependence of the user`s id. But this means you need to add more information into the table where user's information is stored.

QArea
  • 4,955
  • 1
  • 12
  • 22