Results 1 to 2 of 2
Like Tree2Likes
  • 2 Post By espfutbol98

Thread: Login page redirection with variables

  1. #1
    espfutbol98's Avatar
    espfutbol98 is offline x10 Sophmore
    Join Date
    Apr 2009
    Location
    Zagreb... želim
    Posts
    200

    Login page redirection with variables

    This tutorial will show how to protect a page by redirecting to a login page with a return url and if logging out, will show the username that you logged in with. The return url function is standard in ASP.Net but it took me a while to find it in php. This tutorial will not actually show the logging, just redirection and echoing the name in the login page.

    1. First on the protected page, include a securing script. An example is:
    PHP Code:
    <?php
    session_start
    (); 
    if ((isset(
    $_SESSION['uname']) && 
          
    $_SESSION['uname'] != "") || 
        (isset(
    $_SESSION['upass']) && 
          
    $_SESSION['upass'] != "")) {
      
    //If there is a username and a password that is not null, do nothing.
    } else {
      
    $redirect base64_encode($_SERVER['PHP_SELF']); //encodes the location of the page
      
    header("Location: login.php?redirect=$redirect");
      die();
    }
    $redirect base64_encode($_SERVER['PHP_SELF']);
    $name base64_encode($_SESSION['uname']); //used later in logging out
    ?>

    2. In the login.php page, you add something like this:

    PHP Code:
    <?php
      
    if (isset($_GET['redirect'])) {
        
    $redirect $_GET['redirect'];
      } else {
        
    $redirect base64_encode("index.php"); //If there is no redirect, set it to the homepage
      
    }
    //Your login script
    //If login true:
    header ("Location: "base64_decode($_POST['redirect'] ." "));
    ?>
    3. On any link to logout.php:
    PHP Code:
    <?php
    //make sure a securing script like in step 1 is included
    echo "logout.php?redirect='. $redirect .'&name='. $name .'";
    ?>
    4. On logout.php:
    PHP Code:
    <?php
    session_start
    (); 
    unset(
    $_SESSION['uname']);
    unset(
    $_SESSION['upass']);
    //Gets rid of sessions
    if (isset($_GET['redirect'])) {
        
    $redirect $_GET['redirect'];
      } else {
        
    $redirect base64_encode("index.php");
      }
    //If logout.php does not have a "redirect=" in the url, it makes one with the location to index.php.
    if (isset($_GET['name'])) {
        
    $name $_GET['name'];
        } else {
        
    $name base64_encode("");
        }
    //$name is the username in the session that will be echoed in the login form
    header("Location: login?redirect=$redirect&name=$name");
    ?>
    5. Go back to login.php and insert this php code in the value of the unsename <input>. It should look somthing like this:
    PHP Code:
    Username: <input type="text" value="<?php 
    if (isset($_GET['name'])) { 
    $name base64_decode($_GET['name']); //this decodes the "name" from the url
    echo "$name";

    else { echo 
    ""//if it's not set, don't echo anything
    ?>
    " />
    If there are any questions, please ask me. I don't really know how well I explained it. Also, if there are any suggestions or mistakes, please inform me. I had a really huge script and just taking this little part out is a little difficult. What is shown above has not been tested alone but does work properly in my site.
    Enjoy.
    dinomirt96 and karimirt47 like this.

  2. #2
    nirajkum is offline x10 Sophmore
    Join Date
    Mar 2008
    Posts
    160

    Re: Login page redirection with variables

    well thanks for the tutorial really a nice and clean script

Similar Threads

  1. Login Page
    By anivide in forum Scripts, 3rd Party Apps, and Programming
    Replies: 3
    Last Post: 05-13-2009, 10:16 PM
  2. Unable to login X10hosting Login Page
    By kanagala in forum Free Hosting
    Replies: 1
    Last Post: 02-04-2009, 12:21 PM
  3. Not able to access my CPanel login page.
    By Stricken82 in forum Free Hosting
    Replies: 0
    Last Post: 07-22-2008, 02:16 PM
  4. A simple Visual Basic Login Form
    By Zenax in forum Tutorials
    Replies: 0
    Last Post: 03-13-2007, 07:59 AM

Posting Permissions

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