0

I have a login page where the user has to provide his/her credentials and then log in to the page named dashboard.php.

Now the credential validation is done on this page.I used $_POST to pass the variables. If user credentials are not correct then he is taken back to index.php. Else the page is filled with user data from the db.

Now the problem is that when user revists dashboard.php from any other page (other than index.php) How do I get the same credentials back so as to show the contents related to the user? I tried using session variables by saving the credentials in them but still not working.

I have added a sample code of what I have done:

index.php

<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <form action="dashboard.php" method="post">
        <input type="text" name="txt">
        <input type="submit">
    </form>
</body>

this travels to dashboard.php which is:

<?php
    session_start();
   if(isset($_SESSION['LOG_IN'])!=1)
{

    $xt = $_POST['txt'];
    $_SESSION['LOG_IN']=1;

}

?>

<html>
    <head>

    </head>
    <body>
        <p> HI </p>
        <a href="3.php"><?php $xt ?>Link of 3.php</a>

    </body>
</html>

and now when i go to some page called 3.php and press the back button. I get the form re-submission page.

عثمان غني
  • 2,786
  • 4
  • 52
  • 79
  • 1
    The idea is to set your session variables once the user has logged in. At the `dashboard.php` you check if the session variable for logged in is `true`, if not then redirect to `index.php`. On logout, destroy the session. – AyB Mar 04 '14 at 13:23
  • What you want to achieve is the basic of PHP session, get a basic PHP book or check the manual [Session Handling](http://www.php.net/manual/en/book.session.php) – bansi Mar 04 '14 at 13:29
  • you have not started session on the first line of dashboard page – SagarPPanchal Mar 04 '14 at 13:49
  • even after doing that it is not working. Whenever I come back to the page it asks for form resubmission – Manya Sangal Mar 04 '14 at 14:19
  • after session_start() please print_r your $_POST. As I think you are checking isset($_SESSION['LOG_IN']) before defining. – عثمان غني Mar 04 '14 at 14:25
  • Thats the point of isset() to check if it exists or not. Right? – Manya Sangal Mar 04 '14 at 15:31
  • First of all you have to set session variable i.e $_SESSION['LOG_IN']=1; then you can access it or check it anywhere. What you actually want then I can write answer acc. to it. – عثمان غني Mar 04 '14 at 15:56

2 Answers2

0

To make SESSION work on every page you must use session_start(); on each page at the top of page.

Then you can easily access session using print_r($_SESSION);

You should refer to PHP Session Documentation for more info.

You should use session_start(); at top of page. See your question I have updated it.

عثمان غني
  • 2,786
  • 4
  • 52
  • 79
0

First start session_start(); and pass its value $_SESSION['var_name']; and on other (dashboard) page check $_SESSION['var_name'] is set or not ?

you can check this DEMO

Community
  • 1
  • 1
SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62