I have a login system that I build in PHP and MySQL. if any of my users log in they get directed to the user.php page
I have an authenticate.php page that redirects the user based on their ID to a page. This is the code that adds the id to the URL: header(“Location: user.php?id=”.$id); )
JavaScript
x
54
54
1
<?php
2
session_start();
3
// Change this to your connection info.
4
$DATABASE_HOST = 'localhost';
5
$DATABASE_USER = 'root';
6
$DATABASE_PASS = 'root';
7
$DATABASE_NAME = 'phplogin';
8
// Try and connect using the info above.
9
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
10
if ( mysqli_connect_errno() ) {
11
// If there is an error with the connection, stop the script and display the error.
12
exit('Failed to connect to MySQL: ' . mysqli_connect_error());
13
}
14
15
// Now we check if the data from the login form was submitted, isset() will check if the data exists.
16
if ( !isset($_POST['username'], $_POST['password']) ) {
17
// Could not get the data that should have been sent.
18
exit('Please fill both the username and password fields!');
19
}
20
21
// Prepare our SQL, preparing the SQL statement will prevent SQL injection.
22
if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE username = ?')) {
23
// Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s"
24
$stmt->bind_param('s', $_POST['username']);
25
$stmt->execute();
26
// Store the result so we can check if the account exists in the database.
27
$stmt->store_result();
28
29
if ($stmt->num_rows > 0) {
30
$stmt->bind_result($id, $password);
31
$stmt->fetch();
32
// Account exists, now we verify the password.
33
// Note: remember to use password_hash in your registration file to store the hashed passwords.
34
if ($_POST['password'] === $password) {
35
// Verification success! User has logged-in!
36
// Create sessions, so we know the user is logged in, they basically act like cookies but remember the data on the server.
37
session_regenerate_id();
38
$_SESSION['loggedin'] = TRUE;
39
$_SESSION['name'] = $_POST['username'];
40
$_SESSION['id'] = $id;
41
header("Location: user.php?id=".$id);
42
} else {
43
// Incorrect password
44
echo 'Incorrect username and/or password!';
45
}
46
} else {
47
// Incorrect username
48
echo 'Incorrect username and/or password!';
49
}
50
51
$stmt->close();
52
}
53
?>
54
Can I write a javascript function on the original user.php file that will read the user ID in the URL and redirect to a specific page on my site?
Example:
User 1 needs to go
from: http://www.mysite/user.php?id=1
To: http://www.mysite/dashboard/clientA/home.php
User 2 needs to go
from: http://www.mysite/user.php?id=2
To: http://www.mysite/dashboard/clientB/home.php
Advertisement
Answer
Hi you can do the following:
JavaScript
1
12
12
1
//function redirect to page
2
function redirect(){
3
var id= <?php echo $_GET['id'];?>;
4
if(id==1){
5
window.location.href = "url1";
6
}
7
if(id==2){
8
window.location.href = "url2";
9
}
10
}
11
redirect();
12