SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot malluwood's Avatar
    Join Date
    Nov 2006
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy login redirect problem

    hi all

    I have total 3 links.(p1.php,p2.php and p3.php)

    1)when I click on a p1.php,the script has to check whether the user login or not.if login the person will be redirected to a page (for eg:welcome.php).



    2)if not logged in ,login page should show up(login.php) and after success full login,the user has to redirect automatically to welcome.php.


    I have no idea how to implement second part (2).please help me


    Thanks

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if ($logged_inheader('Location: http://mysite.com/welcome.php');
    else 
    header('Location: http://mysite.com/login.php');
    exit(); 

  3. #3
    SitePoint Zealot malluwood's Avatar
    Join Date
    Nov 2006
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks,I agree this .But that is not the issue here
    suppose if i am having around 10 links in my page.I clicked on one of the link
    at that time if the user not logged in,the user will be redirected to login page.then after suceesfull login user must be redirected to that particular link (I mean the link which the user clicked first)

    how can i achieve this
    please help me

  4. #4
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put a logged_in flag in the session.

    Include this in a separate file at the start of every page:
    PHP Code:
    session_start();
    if (!
    session_is_registered('logged_in')) {
     
    session_register('logged_in');
     
    $_SESSION['logged_in'] = false;
    }
    if (
    $_SESSION['logged_in'] === false) {
     
    header('Location: login.php');

    Then in your login script, put:

    PHP Code:
    // login() returns true on success, false on failure

    if (login($_POST['username'],$_POST['password'])) {
     
    $_SESSION['logged_in'] = true;


  5. #5
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops. This part should be used only on the protected pages, not the public ones.

    Quote Originally Posted by World Wide Weird View Post
    PHP Code:
    if ($_SESSION['logged_in'] === false) {
     
    header('Location: login.php');


  6. #6
    SitePoint Zealot malluwood's Avatar
    Join Date
    Nov 2006
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but after suceesfull login user must be redirected to that particular link (I mean the link which the user clicked first)

    how that is happening here?

  7. #7
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can temporarily store the destination URL in the session also and send them there when the login succeeds. In fact, you can persist just about any kind of data between pages in the session. Brush up on the session functions in the PHP manual. They're fairly simple and straightforward.

    http://www.php.net/manual/en/ref.session.php

  8. #8
    SitePoint Zealot malluwood's Avatar
    Join Date
    Nov 2006
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can anybody help me?

  9. #9
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by malluwood View Post
    can anybody help me?
    Dude, read my previous post. This is something you can figure out on your own with a little research. No offense, but most people don't have time to spoon feed you code all day.

    If $_SESSION['logged_in'] is false, put the destination URL in another session variable, say $_SESSION['dest_url'], do the login, and if it succeeds, copy $_SESSION['dest_url'] to the Location header as previously described.

  10. #10
    SitePoint Zealot malluwood's Avatar
    Join Date
    Nov 2006
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had clearly mentioned my problem.but you confused me

    thanks

  11. #11
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let me break it down into steps and see if that helps.

    First, start the session with session_start() at the beginning of each page before you do anything else.

    Second, register and initialize all the session variables you will be using. Example:

    PHP Code:
    session_start();
    if (!
    session_is_registered('sessVar1')) {
    session_register('sessVar1');
    $_SESSION['sessVar1'] = "some default value";
    }
    if (!
    session_is_registered('sessVar2')) {
    session_register('sessVar2');
    $_SESSION['sessVar2'] = "some default value";
    }
    .
    .
    .

    if (!
    session_is_registered('sessVarN')) {
    session_register('sessVarN');
    $_SESSION['sessVarN'] = "some default value";

    You can put all of the above in an include file and include() it in every script.

    In your scripts that require the user to be logged in, check if $_SESION['logged_in'] is true. If not, put the script's URL in $_SESSION['dest_url'] and use header('Location: login.php') to send them to the login page.

    During the login process, if the login is successful, set $_SESSION['logged_in'] = true, then do this:

    PHP Code:
    $dest $_SESSION['dest_url'];
    $_SESSION['dest_url'] = "";
    header(Location' . $_SESSION['dest_url']); 
    Anytime the user tries to view a secured page, it should check $_SESSION['logged_in'] before displaying the page, and store the destination URL and send the redirect header if they're not logged in.


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
  •