Check if the user is logged in or not.

I am making a web application that uses PHP to find a job.

I have one request; when the user is not logged in and wants to apply for the task by clicking the "apply" button, he is redirected to the login page. If the user is logged in when clicked, they should go directly to the app page. I'm not sure how to implement this.

I am confused because I am new to PHP.

+2


source to share


6 answers


Your question is very vague - maybe start with Authentication in PHP



+3


source


You can store the value in a session called Login and set it when the user logs in. This can also be used to redirect the user if they are not logged in:



<?php

  // check that the session variable does exist
  // check that the user 'LoggedIn' has been set to 1 (true)
  if (!isset($_SESSION['LoggedIn']) && $_SESSION['LoggedIn'] != 1)
  {
       // redirect to login page for user to authenticate themselves.
       // pass page location (with parameters if necessary) to redirect
       // the user on successful login.
       header("Location: Login.php?redir=ApplyForJob.php?JobID=12345");
  }
  else
  {
       // user is logged in
       // redirect the user directly to the apply for job page.
       header("Location: ApplyForJob.php?JobID=12345");
  }

?>

      

+1


source


Ok, when the user clicks "apply" in your application, the user is redirected to the login page, if not logged in (which you can check if the user session exists ), remember when you redirect the page send the url of the current page in the options to the login page so that when the user logs in it can be redirected back to the previous page and click to apply for this particular job .....
This is how the logic works, if you want php, mysql explanation, you it will take some time for you to understand, as you yourself admitted that you are new to php ..

+1


source


Can you, when a user logs in, assign a variable to that user $_Session

? that is, after authenticating, you set the variable $_SESSION['user']

.

$_SESSION['user']='admin';

      

So, if you want to check if the user will already be logged in after that, just use this:

if(isset($_SESSION['user']))
{
  // user is login, direct to the job page
}

else
{
  // no login, go to the login page
}

      

0


source


Each page sets the cookies or session they were just on:

$expire=time()+60*60*24*30;
setcookie("wherewasi","",time() - 1000);
setcookie("wherewasi",$_SERVER['REQUEST_URI'], $expire);

      

Then after logging in to redirect them:

$loc = ($_COOKIE['wherewasi'])?$_COOKIE['wherewasi']:'index.php';
header("location: ".$loc);
exit();

      

0


source


There are two things you need to worry about ... checking that they are logged in, and then after they are logged in, directing them to the correct page.

It's all about "persisting state" in page requests. To do this, you need to use cookies or more useful sessions (which can be made using cookies or processed by the PHP engine for you automatically).

Sessions are probably a good way to go. To use sessions, each page must start with

<?php session_start(); ?>

      

at least before any html code that is written to the browser.

After that you can use your session variable to store

<?php $_SESSION['user']='joe_blow'; ?>

      

(and check)

<?php
  if(isset($_SESSION['user']) && $_SESSION['user']!='' {
     // do something
  }
?>

      

whether the user is registered and to which page they should be redirected after logging in.

<?php header("location: ".$_SESSION['redirect_location']));

      

But in order to write more useful code, I think people should know which authentication method you used ... (How do you make your login? Do you store the ID in the database? Shelf package?)

0


source







All Articles