Question about PHP login script

Posted In PHP
  • nicolaj-arberg 3 years ago

    Hello, just saw your PHP login scrit video on youtube and liked it very much. It is also working 100%.

    I am just wondering, if i want a page only displayed to those who are logged in. What should i do? And of course if they are not logged in they will go to my index.php.

     

    Thanks for the nice video and hopefully an awnser in here! :-D

  • devdojo 3 years ago

    Hey @Nicolaj-arberg,

    What you could do is inside of index.php after this section:

     

    <?php
    session_start();
    require 'database.php';
    if( isset($_SESSION['user_id']) ){
    	$records = $conn->prepare('SELECT id,email,password FROM users WHERE id = :id');
    	$records->bindParam(':id', $_SESSION['user_id']);
    	$records->execute();
    	$results = $records->fetch(PDO::FETCH_ASSOC);
    	$user = NULL;
    	if( count($results) > 0){
    		$user = $results;
    	}
    }
    ?>
    

    You can add this:

     

    <?php
    
    if( !empty($user) ){
        header( 'Location: http://www.yoursite.com/authenticated.php' );
    }

    And this will automatically redirect an authenticated user to the authenticated.php page if they are logged in.

    So, now you can create an `authenticated.php` file which will look like the following:

     

    <?php
    session_start();
    require 'database.php';
    if( isset($_SESSION['user_id']) ){
    	$records = $conn->prepare('SELECT id,email,password FROM users WHERE id = :id');
    	$records->bindParam(':id', $_SESSION['user_id']);
    	$records->execute();
    	$results = $records->fetch(PDO::FETCH_ASSOC);
    	$user = NULL;
    	if( count($results) > 0){
    		$user = $results;
    	}
    }
    ?>
    
    <?php 
    if($user == NULL){
        header( 'Location: http://www.yoursite.com/index.php' ) ;
    }
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
    	<title>Welcome <?= $user['email']; ?></title>
    	<link rel="stylesheet" type="text/css" href="assets/css/style.css">
    	<link href='http://fonts.googleapis.com/css?family=Comfortaa' rel='stylesheet' type='text/css'>
    </head>
    <body>
    
    	<div class="header">
    		<a href="/">Your App Name</a>
    	</div>
    	
    	<br />Welcome <?= $user['email']; ?> 
    	<br /><br />You are successfully logged in!
    	<br /><br />
    	<a href="logout.php">Logout?</a>
    
    </body>
    </html>

    Hope this helps you out :)

    Thanks!

  • susan-byrd 2 months ago
    I warmed welcome you to your first blog and I respect your honor for leaning PHP skills and doing a project on it and really your article is very impressive as for the first time and you can check [https://edubirdie.net/guarantees](https://edubirdie.net/guarantees) in order to manage the task. Do share and bring up new aspects on different topics.

    I warmed welcome you to your first blog and I respect your honor for leaning PHP skills and doing a project on it and really your article is very impressive as for the first time and you can check https://edubirdie.net/guarantees in order to manage the task. Do share and bring up new aspects on different topics.

Please login or signup to leave a response.

Login

or Sign Up

Sign Up

or Login
×
Want to create your own SAAS?

I can teach you to create your own Software as a Service. Click here to learn more!

Visit SAAS Adventure!