1

I have a main page that has a log in link on it that takes the user to a login page. However, if a user is already logged in I want to take the user to another page, for example a info page.

I have this function:

function logged_in_redirect() { if (logged_in() === true) { header('Location: client.php'); exit(); } } 

I'm wondering where to put this? I've tried pretty much everything. If i put this on the login page, it does not redirect me when i am logged on. I've tried adding it as an onclick function for the link on the home page but it didn't work.

This is my logged_in() function:

function logged_in() { return (isset($_SESSION['user_id'])) ? true : false; } 

Any suggestions?

Edit:

I have currently fixed the problem by making the button on the home page link to a test.php file which has this code:

<?php include 'core/init.php'; if (isset($_SESSION["user_id"])) { header('Location: client.php'); } else { header('Location: info.php'); } ?> 

Is there any way around this?

6
  • 1
    Uhm, is this PHP or I am missing something? The question is tagged as Javascript. Commented May 12, 2013 at 22:21
  • What is this show you? echo var_dump(logged_in()); Commented May 12, 2013 at 22:21
  • This is completely and totally PHP. Commented May 12, 2013 at 22:23
  • echo var_dump(logged_in()); = bool(true) Commented May 12, 2013 at 22:27
  • header('Location: client.php'); <-- the Location header is only valid for absolute URLs. While browsers are usually lenient in not requiring a full URL, you should never rely on relative URLs. Change to header('Location: http://'.$_SERVER['HTTP_HOST'].'/client.php'); Commented May 12, 2013 at 22:34

1 Answer 1

1

If your session is set and the user is properly authenticated this will work.

You don't need extra function to check whether login is set unless you have a common file which is handling authentication related stuff and all the other files calling its function to check if the user is logged in..

Login Page:

<?php //check if the user is already loggedin if(isset($_SESSION["user_id"]){ //assuming client.php is in the same directoy header("Location: client.php"); //you don't need exit since it will be redirected } //your login stuff. if your user_id was not set this part will be executed ?> 

Also don't forget to destroy session once you log out..

Sign up to request clarification or add additional context in comments.

4 Comments

That still does not work for some reason. It always redirects to info page and not client.php even though I am logged in.
what do you have on client page?.. Assuming your info page is main page.. this code sits on login page so if session is set it redirects to client php and if something is redirecting to info page on client.php you have double redirection..
i think to make thingss more clear to us you should provide some more code.. client.php and login.php may be..
Right now i have a link on home page to info/login page. If the link is clicked it directs to info/login page. However, if the user is already logged in there is no need to go to info/login page so i want to redirect to client.php

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.