Hello All

I want the same feature as DaniWeb have.
If I Go on PHP forum and try to start new discussion without doing login process, it takes me to the login page and if enter my valid login id and password it directly takes me to page where I can start posting new post.

In my site I want to implement same facility.
I have already done login part if user click with doing login process on a link which requires login validation process then user will get a message for Enter login information....
Now I am not find the way "How I will take the user back to that same page where he/she want to go after his/her valid login".

Need guidance me to do this

Thank you for any support…

In your restricted page enable a $_SESSION to record the $_SERVER['REQUEST_URI'], for example:

<?php
if(!isset($_SESSION['logged'])) # this is where you check if access is allowed
{
    $_SESSION['destination'] = $_SERVER['REQUEST_URI'];
    header('Location: login.php');
}
else
{
    if(isset($_SESSION['destination']))
    {
        unset($_SESSION['destination']);
    }
}
?>

And in you login script, after you validate the credentials replace your redirect with this:

if(isset($_SESSION['destination']))
{
    header('Location: '.$_SESSION['destination']);
}
else
{
    header('Location: default_restricted.php');
}

bye!

Hi cereal
I tried to understand what you suggested and I tries to implement it in my code but not working (Confused how to integrate your code in my code), can you please give a review on my code presented here -
CODE

<?php
        if(!defined('HOST'))include 'dbconnection.php';
         if(login_check($mysqli) == true){ ?>
         <div id="nav" class="image002-03">
         <span id="smalltext" style="bottom: 0px; margin-bottom: 0px; padding-bottom: 0px; font-family: Calibri; font-size: large; text-align: center;">Service Menu</span>
        <ul id="ul1" class="serviceul">
            <li class="serviceli"><a href="unsolvedCSQPXI.php">Unsolved Question Papers</a></li>
            <li class="serviceli"><a href="unsolvedCSSPXI.php">Unsolved Sample Paper</a></li>
            <li class="serviceli"><a href="">Projects Samples</a></li>
            <li class="serviceli"><a href="">Presentations</a></li>
            <li class="serviceli"><a href="">Uploads</a></li>
            <li class="serviceli"><a href="downloads.php">Solved Materials</a></li>
            <li class="serviceli"><a href="forum.php">Forum</a></li>
            <li class="serviceli"><a href="">Live Chat</a></li>        </ul>
</div>
<?php
}
else{?>

<div id="nav" class="image002-03">
        <span id="smalltext" 
            style="bottom: 0px; margin-bottom: 0px; padding-bottom: 0px; font-family: Calibri; font-size: large; text-align: center;">Service Menu</span>
        <ul id="ul1" class="serviceul">
            <li class="serviceli"><a href="unsolvedCSQPXI.php">Unsolved Question Papers</a></li>
            <li class="serviceli"><a href="unsolvedCSSPXI.php">Unsolved Sample Paper</a></li>
            <li class="serviceli"><a href="">Projects Samples</a></li>
            <li class="serviceli"><a href="">Presentations</a></li>
            <li class="serviceli"><a href="invalidentry.php">Uploads</a></li>
            <li class="serviceli"><a href="invalidentry.php">Solved Materials</a></li>
            <li class="serviceli"><a href="invalidentry.php">Forum</a></li>
            <li class="serviceli"><a href="invalidentry.php">Live Chat</a></li>        </ul>
</div>
<?php } ?>   

Let's simplify, in your code you have:

if(login_check($mysqli) == true)
{
    # restricted
}
else
{
    # public
}

so, login_check() performs a query to match user and password each time and for each restricted block in your pages? Is this function included inside dbconnection.php? If yes, do you store submitted username and password somewhere like $_SESSION or $_COOKIES?

In my example you have to use session_start(); on top of each restricted page and also inside the login page, and I'm assuming you use a $_SESSION to enable access into a restricted area, as example $_SESSION['logged']. But your approach seems to be different. How does your login script works, can you post that code? (remember to remove database username, password and hostname!)

Dear Cereal
My pages are implemented bit different way, if you can spare sometime and take some botheration for me on viewing my last post(solved) under category PHP forum then you will come to know the detail. Here is the post link Click Here

If you want a redirect then you have to restrict an entire page, not a block, you can append this script at the top of a page which is allowed only to logged users:

include 'dbconnection.php';
include 'functions.php';
if(login_check() === false)
{
    $_SESSION['destination'] = $_SERVER['REQUEST_URI'];
    header('Location: login.php');
}
else
{
    if(isset($_SESSION['destination']))
    {
        unset($_SESSION['destination']);
    }

    #
    # restricted page
    # include/echo or just redirect
    echo 'hello';

}

and change line 10/11 in your process_login.php of the posted script:

if(login($email, $password, $mysqli) === true)
{
    // Login success
    if(isset($_SESSION['destination']))
    {
        header('Location: '.$_SESSION['destination']);
    }
    else
    {
        include("XICS.php");
    }

}
else
{
    // Login failed
    header('Location: login.php?error=1');
}

Try this, but you can also see if someone of the users of the other thread will continue to help you here, they will certainly have a better picture of the project than me.

then how in daniweb they are doing?
We can see entrire page we can move around the forums without login but if we want to post then login require

You can browse the forum because only the script used to save the post is accessible to qualified users. When a user sends a post, then the script checks for the referer and redirects the user back to the original thread. In this case you will need $_SERVER['HTTP_REFERER'] or as in daniweb you will need an hidden input field which sends the link of the thread as value, this is used to redirect and to get category, subcategory, thread id...

In the case, for example on daniweb, you want to access directly to your edit_profile which is a restricted page, but the session ends or you logged out, you will need to save the $_SERVER['REQUEST_URI'] and redirect the user to the login page, from which then you go to the saved destination.

Bye!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.