Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Redirect Multiple Users

Codes here !

Moderators: macek, egami, gesf

Redirect Multiple Users

Postby RobertCraigUK » Tue Apr 17, 2012 11:02 am

Hello,

I have been trying for the last few days to figure out how to send a particular user to their own specific URL. For example, all users will use the same login.php page but when User A logs in they will be redirected to A.php and if User B logs in they will be redirected to B.php etc.

I am totally new to php and have been using it for about 3 weeks. I am very familiar with html. So far I have managed to create a script where the above redirection works but it is not connected to a database. The redirection is all hard-coded into the script.

This is the code which is currently working:

Code: Select all
<?php
session_start();
$users['david'] = array('password' => '123', 'redirect' => 'david.php');
$users['peter'] = array('password' => '321', 'redirect' => 'peter.php');
if(array_key_exists($_POST['username'],$users)) {
    if($_POST['password'] == $users[$_POST['username']]['password']) {
        $_SESSION['loggedIn'] = true;
        header('Location:'.$users[$_POST['username']]['redirect']);
        exit();
    }
else {
    // invalid password
    header('location: login.php');
    exit;
    }
}
else {
    // invalid username
    header('location: login.php');
    exit;
}
?>


This works 100% and is almost exactly what I need. The user gets redirected to their own specific URL. The only problem is that the code is not connected to a database and the users, password and redirection are all hard-coded within the script. Is there anyway to adapt the above code so it would connect to a database for the information? I have phpmyadmin installed.

Any help whatsoever would be much appreciated and I would be truly grateful if someone could please help me.

Kind regards,

Robert.
RobertCraigUK
New php-forum User
New php-forum User
 
Posts: 19
Joined: Tue Apr 17, 2012 10:52 am

Re: Redirect Multiple Users

Postby minimihi » Tue Apr 17, 2012 12:18 pm

Database is meant to have information stored in it.

Since you have PhpMyAdmin, just create database, then user who could connect to it with all rights.
Inside of database create table 'users' which could have such structure:
user_id, type: INT, it's appropriate to make IDs auto increment (A_I);
username, type: VARCHAR (you need to set symbol limit for this one and max is 255);
password, type: VARCHAR;
redirect, type: VARCHAR;

Insert users data into table.

Set up user log in form in your HTML or PHP file.

Retrieve GETs/POSTs in your PHP file, after submitting form.
Connect to mySQL and chose database via PHP.
Make mySQL SELECT query to get if user exist on 'users' table and his log in data is correct. At same time retrieving data you will need.
Set up your SESSION and header according data you received from SELECT.

That's how php could look when using table structure I suggested above:
Code: Select all
<?php
// connection to database
$server = mysql_connect('server_address', 'your_database_username', 'your_database_user_password');
mysql_select_db('database_name', $server);

// data from user log-in form
$user_name = $_GET['name'];
$user_password = $_GET['pass'];

// formating query string
$qry = "SELECT user_id, redirect
    FROM
        users
    WHERE
        username = '"
.$user_name."' AND
        password = '"
.$user_password."'";
// calling query
$result = mysql_query($qry);
// how many rows where SELECTed
$count = (int)mysql_num_rows($result);
if(
$count != 0) {
    // log-in correct
    
    
//in this case we need to take out only one row, so no need for loop
    $row = mysql_fetch_assoc($result);
    
    header
('Location: '.$row['redirect']);
}
 else {
    // log-in incorrect
    header(Location: login.php);
}

// bye bye
mysql_close($server);
?>
Last edited by minimihi on Tue Apr 17, 2012 2:54 pm, edited 3 times in total.
User avatar
minimihi
New php-forum User
New php-forum User
 
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: Redirect Multiple Users

Postby RobertCraigUK » Tue Apr 17, 2012 2:18 pm

Thank you minimihi for your reply.

I have tried the code but I am currently getting redirected to login.php even if I enter the correct login details. Is there anything simple which I have done incorrectly?
RobertCraigUK
New php-forum User
New php-forum User
 
Posts: 19
Joined: Tue Apr 17, 2012 10:52 am

Re: Redirect Multiple Users

Postby minimihi » Tue Apr 17, 2012 2:44 pm

After all I wrote it without testing :D it would have been miracle if there weren't any errors

$count is string and 0 is integer, so there are several ways to fix it
Code: Select all
$count = (int)mysql_num_rows($result); 

or
Code: Select all
if((int)$count != 0) 

or
Code: Select all
if($count != '0') 


*fixed it on whole code
User avatar
minimihi
New php-forum User
New php-forum User
 
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: Redirect Multiple Users

Postby RobertCraigUK » Wed Apr 18, 2012 10:24 am

Thanks again for the reply minimihi, it is very much appreciated.

I have tried the new code but I am still getting redirected to login.php even if I enter the correct login details.

In the redirect field of my database I have entered the page the user should be redirected to, eg. "enter5.php" and that is it, is this correct?
RobertCraigUK
New php-forum User
New php-forum User
 
Posts: 19
Joined: Tue Apr 17, 2012 10:52 am

Re: Redirect Multiple Users

Postby minimihi » Wed Apr 18, 2012 10:59 am

Code is good, I tested it when there was error.

The problem might be at how you integrate my code into yours or in table/selection.

Try to print out number of selected rows when you enter correct user log-in details. If it's correct, you will receive 1, if not - 0
You could do that by entering
Code: Select all
echo "<pre>"; var_dump($count); echo"</pre>"; die(); 

right after this line
Code: Select all
$count = (int)mysql_num_rows($result); 


also do var_dump() with $user_name and $password to see if you receive data from your log-in form.

Yes.
In redirect field enter file;
In username field - log-in username;
In password field - log-in password.

By the way, if you used same table structure as my sample structure, in code I posted you need to change only these:
server_address, your_database_username, your_database_user_password and database_name

When you log-in with same data as in username and password fields, you will be redirected to file in redirect field.
User avatar
minimihi
New php-forum User
New php-forum User
 
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: Redirect Multiple Users

Postby RobertCraigUK » Wed Apr 18, 2012 12:38 pm

When I enter the correct login details with the echo code the result is int(0)

So does this mean that there is a problem with the database? I have changed the database host, username and password so it is not that.

I have checked and doubled checked and cannot see anything wrong with the database? :?
RobertCraigUK
New php-forum User
New php-forum User
 
Posts: 19
Joined: Tue Apr 17, 2012 10:52 am

Re: Redirect Multiple Users

Postby minimihi » Wed Apr 18, 2012 1:09 pm

put
Code: Select all
die($qry); 

right after
Code: Select all
$result = mysql_query($qry); 


Tell me what output you will get on the screen.
You should get query string. I wanna see if it's correct, if you're receiving anything from log-in form
User avatar
minimihi
New php-forum User
New php-forum User
 
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: Redirect Multiple Users

Postby RobertCraigUK » Wed Apr 18, 2012 1:18 pm

I get this message:

SELECT user_id, redirect FROM users WHERE username = '' AND password = ''
RobertCraigUK
New php-forum User
New php-forum User
 
Posts: 19
Joined: Tue Apr 17, 2012 10:52 am

Re: Redirect Multiple Users

Postby minimihi » Wed Apr 18, 2012 1:45 pm

You're not getting log-in form data.

If you won't succeed in fixing your problem after checking out those two links below, post your log-in form and PHP code here.

Do you know how to use HTML form? How to pass data via GET/POST method?

Reference:
$_GET
$_POST
User avatar
minimihi
New php-forum User
New php-forum User
 
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: Redirect Multiple Users

Postby RobertCraigUK » Wed Apr 18, 2012 1:59 pm

Yes I am familiar with html forms. I am using the POST method in my form which I assumed was the correct method?

Code: Select all
<form action="login.php" method="POST">
Username:
<br />
<input type="text" name="username" />
<br /><br />
Password:
<br />
<input type="password" name="password" />
<br /><br />
<input type="submit" name="submit" value="Log In" />
</form>


This is the index.php and that is all the code I have on this page.

The I have a login.php page which has your code. And finally I have a enter.php which is a blank page with welcome on just to test, it's the page I have in my redirect field in my database.
RobertCraigUK
New php-forum User
New php-forum User
 
Posts: 19
Joined: Tue Apr 17, 2012 10:52 am

Re: Redirect Multiple Users

Postby minimihi » Wed Apr 18, 2012 2:58 pm

Since you're using POST, in my code you had to change $_GET to $_POST. Did you do that?

If your HTML form method is "post", then in PHP code you have to take data from HTML form with $_POST.
If method is "get" - take it with $_GET

Well, there actually is one more variable which receives both of them + $_COOKIE. But since I don't like it, I won't encourage to use it, especially when you know in which method data is passed.
User avatar
minimihi
New php-forum User
New php-forum User
 
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: Redirect Multiple Users

Postby RobertCraigUK » Thu Apr 19, 2012 12:59 pm

Wow I've finally managed to get it working. Thank you for all you help and I'll continue checking out the site for more tips as I am a real newbie to php.

Thanks again!!
RobertCraigUK
New php-forum User
New php-forum User
 
Posts: 19
Joined: Tue Apr 17, 2012 10:52 am

Re: Redirect Multiple Users

Postby RobertCraigUK » Fri Apr 27, 2012 4:31 pm

Hi,

Using the above code how easy would it be to create a link on every page saying something like "My Account" - and then the script knowing who is logged on and redirect to the users page.

The redirect field is already in phpmyadmin so that part is there, it's just knowing the code of the link.

How easy is this? Or would it require a lot of coding?

Any help would be appreciated
RobertCraigUK
New php-forum User
New php-forum User
 
Posts: 19
Joined: Tue Apr 17, 2012 10:52 am


Return to mySQL & php coding

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron