Skip to content Skip to sidebar Skip to footer

How To Redirect User To Particular Link After Login In Php

I want to know how to redirect user to particular link after login in php for example in e-commerce website if user click on add to cart button and if that user is not logged in th

Solution 1:

First check user is login or not

if(isset($_SESSION['username'])){
$url='cart.php';
}else{
$url='login.php';
}
<ahref="<?phpecho$url; ?>">add to cart</a>

Solution 2:

Probably it would be quicker for you to Google this first than to take the time typing it into StackOverflow and waiting to monitor responses, but who's to say. Maybe your browser was already open on this page and you're using an old dialup modem so loading new non-cached content is very slow.

How to make a redirect in PHP?

Solution 3:

you can try this one:

header('Location: http://www.example.com/');

Here you can find useful links to free books:https://stackoverflow.com/tags/php/info

Solution 4:

You have to pass the variables for your reference to know from which page you are coming.

You have to use header() for this.

session_start();// beginning of your file.if(user is logged in) {
     start checking for conditions here.
     if($_GET['from'] == "checkout"$file="checkout.php";
     else$file="index.php";

     header("Location: $file");// file to which you want to redirect after login.
} else {
     // redirect to login page.
}

You can pass from where you are coming to this page, then check that is user is logged in, then decide where to redirect.

Solution 5:

login form

<formaction="loginchk.php"method="post"><inputtype="text"name="unm"id="unm" /><inputtype="password"name="pwd"id="pwd" /><inputtype="hidden"name="redirect_url"value="<?phpecho basename($_SERVER['PHP_SELF']); ?>" /><inputtype="submit"value="Login" /></form>

loginchk.php

<?phpif(!empty($_POST)){
        extract($_POST);

        //sql queryif(//true condition){
              header("location:".$redirect_url);
        } else{
              header("location:login.php");
        }
    }
?>

Post a Comment for "How To Redirect User To Particular Link After Login In Php"