SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    If Not Logged In Redirect

    Hi,

    I am trying to complete a registration script. There are certain pages which I want to redirect someone if they not logged in. Can anyone please advise how best to do this?


    Code:
    	 <?php
    	     if ($_SESSION['userLoggedIn'])
    		 
    	
    		 
    {
      echo '<div class="loggedin">
    ' . $_SESSION['userfirstname'] . ' ' . $_SESSION['usersurname'] . ' <a href="/test/closesession.php">Logout</a> 
      </div>
      ';
    } else { echo 
    '<div class="headersignin">	
    <a href="/users/login.php"   rel="nofollow" class="blacklink"   >
    Sign in
    </a>
    </div>
    <div class="headerjoin">
    <a href="/users/register.php" rel="nofollow" class="whitelink"   >	Join free</a>
    </div>';
    }
    ?>

  2. #2
    SitePoint Enthusiast Strider64's Avatar
    Join Date
    Aug 2012
    Posts
    37
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    With a registration page you wouldn't want them not to be able to register? Just prevent them from going to private pages by adding something like the following to those pages.

    PHP Code:
    <?php

        
    // First execute a common code to connect to the database and start the session
        
    require("includes/common.php");
        
        
    // At the top of the page check to see whether the user is logged in or not
        
    if(empty($_SESSION['user']))
        {
            
    // If they are not, redirect them to the login page.
            
    header("Location: login.php");
            
            
    // Remember that this die statement is absolutely critical.  Without it,
            // people can view your members-only content without logging in.
            
    die("Redirecting to login.php");
        }

  3. #3
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks mate,

    I got this to work within a few mins.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •