Creating Specific redirect after user loged in

Discussion in 'PHP' started by lamelime, Dec 10, 2008.

  1. #1
    Hey guys,

    I am having trouble creating a specific redirect that lets users be redirected to a specific page, based on their username. For example, digital point, allows users to be redirected to their own profile after user is authenticated. I want to be able to do that so I can create custom profiles for each member that signs up for my site.

    Any help would be appriciated!

    Thanks! :D
     
    lamelime, Dec 10, 2008 IP
  2. GreatMetro

    GreatMetro Peon

    Messages:
    117
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Well, you can create a php redirect using the header() function.

    Try re-directing with a get variable:

    <?php
    if (logged_in($user_id)) {
    $location = "profile.php?id=" . $user_id;
    header("location: $location");
    }
    ?>
     
    GreatMetro, Dec 10, 2008 IP
  3. tjsocr22

    tjsocr22 Active Member

    Messages:
    1,251
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Thanks for the tip, I will try it out!

    Where should I replace the variables? like this?

    <?php
    if(logged_in(tjw1)) {
    $location = "profile.php?id=22" . tjw1
    header("loaction :$location");
    }
    ?>
     
    tjsocr22, Dec 11, 2008 IP
  4. chopsticks

    chopsticks Active Member

    Messages:
    565
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Whats tjw1?

    Is that meant to be a variable (i.e $tjw1) or is it a constant holding some other value?

    If you stored the users id in some variable just use that in your redirect. As such with posted above, but the method in your post redirects to a fixed id, not a dynamic one from the logged in user.

    So with GreatMetro's code, if $tjw1 is your variable containing the user id, then use as such:

    <?php
    if ( logged_in($tjw1) ) 
    {
      $location = "profile.php?id=" . $tjw1
      header("location: $location");
    }
    ?>
    PHP:
    If the page you are redirecting too isn't actually profile.php, then change that accordingly.
     
    chopsticks, Dec 11, 2008 IP
  5. GreatMetro

    GreatMetro Peon

    Messages:
    117
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Also, logged_in() is a custom function you would write to check if someone is logged in.

    You could also do it with session variables:

    session_start();
    if (user($user_id)) {
    $_SESSION['uid'] = $user_id;
    header("location: profile.php");
    }

    Then on the profile.php, you would also start the session, and check the $_SESSION['uid'] to pull the correct profile information. Using sessions is much more secure than using GET variables.
     
    GreatMetro, Dec 12, 2008 IP
  6. chopsticks

    chopsticks Active Member

    Messages:
    565
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Using GET it fine in this case, just sanitize the data correctly, and in the case of a profile, type cast it to int and your set. Besides that anyway, GET would need to be used if someone else was viewing the profile.
     
    chopsticks, Dec 12, 2008 IP