I created a login system where after login, the user will be redirected to profile.php
. In the profile.php
page, the heading will say Welcome [username]
Session.php:
$connection = mysqli_connect("localhost", "root", "");
$db = mysqli_select_db($connection, "id2290767_wp");
session_start();
$user_check = isset($_SESSION['login_user']);
$ses_sql = mysqli_query($connection, "select name from login where name='$user_check'");
$row = mysqli_fetch_assoc($ses_sql);
$login_session = $row['name'];
if(!empty($login_session)) {
mysqli_close($connection);
header('Location: members.php');
}
profile.php (the one with the greetings)
<?php
include('session.php');
if(!isset($_SESSION['login_user'])){
header("location: profile.php");
}
?>
<h1>Welcome <i><?php echo $login_session; ?></h1>
The signin.php linked with the login form in members.php:
session_start(); // Starting Session
$error = ''; // Variable To Store Error Message
if(isset($_POST['submit'])) {
if(empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
} else {
// Define $username and $password
$username = $_POST['username'];
$password = $_POST['password'];
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysqli_connect("localhost", "root", "");
// To protect MySQL injection for Security purpose
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($connection, $username);
$password = mysqli_real_escape_string($connection, $password);
// Selecting Database
$db = mysqli_select_db($connection, "id2290767_wp");
// SQL query to fetch information of registerd users and finds user match.
$query = mysqli_query($connection, "select * from login where password='$password' AND name='$username'");
$rows = mysqli_num_rows($query);
if($rows == 1) {
$_SESSION['login_user'] = $username; // Initializing Session
header("location: profile.php"); // Redirecting To Other Page
} else {
$error = "Username or Password is invalid";
}
mysqli_close($connection); // Closing Connection
}
}
members.php:
<?php
include('signin.php'); // Includes Login Script
if(isset($_SESSION['login_user'])){
header("location: profile.php");
}
But I get no text in the PHP page. It just says "Welcome".
What did I do wrong?
EDIT: Getting infinite loop after changing
$user_check = isset($_SESSION['login_user']);
to
$user_check=isset($_SESSION['login_user']) ? $_SESSION['login_user'] : "";