0

I'm slowly learning PHP ;-) I'm having difficulties understanding how separate PHP-files work together.

I make AJAX calls to different php files that all need to be connected to the backend (Parse). Such as:

sign_up.php
login.php
verify_email.php
get_something_out_of_the_database.php

What is the standard way to stay logged in over the different php files? (or what is the google search term for it..?)

Update: Thanks for all your answers about 'sessions'. I doesn't work very well yet, so i made a new question.

Thanks!

Remzo

3
  • You must create a file and initialize the session in it, as session_start() and include this file, anywhere you need to authenticate a user or provide some thing base on sessions Commented Jan 25, 2017 at 14:06
  • Use SESSIONS Commented Jan 25, 2017 at 14:06
  • stay logged in over the different php files how about bootstraping your application. Means: every call is redirected to index.php and there you can check for loggin and so on. Thats how frameworks do it. OldSchool but also works: use autoprepend function from php php.net/manual/en/ini.core.php#ini.auto-prepend-file and do the checks there.(less code work for you) Commented Jan 25, 2017 at 14:08

3 Answers 3

1

You should use PHP sessions. These are a way to store information on visitor browser between multiple pages...

To start a session, you first need to add session_start(); in every PHP file you intend to use it. Usually it's added in a header.php

Then, you can use sessions already.

To store a result:

$_SESSION['some_data'] = $var;

To retrieve a result in another page, for example:

echo $_SESSION['some_data']; // will echo $var

More info can be found here: http://www.w3schools.com/php/php_sessions.asp

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

Comments

0

You can do this for example by storing the login-data in a session-variable and checking it at the start of every new page.

Example: You check if login-data is valid. Then

session_start();
$_SESSION["login"] = $loginname;

At the start of another page:

session_start();
if(!isset($_SESSION["login"]) || $_SESSION["login"] != "check_somehow")
{
  header("Location: logout.php");
  exit;
}

For logging out you can use

session_start();
session_destroy();

3 Comments

Thanks!! If i understand it correctly, this solutions transfers user data in a variable to another php file. But suppose this other php file needs to get something out of the database. Does it then have to log itself in again? And is it then logged in twice...?
This starts a session which is available across all the php files, now you can validate this session by checking diffrerent values set( as per your requirements). You don't need to logging again in the same session
I've tried the session_start for a few hours now. But ... it doesn't work for me in combination with calling PARSE. AAAAAAAHHHHHH. If i remove session_start from my php file, i can retrieve info from the Parse database. I don't have the Session variables then. If i add session_start() i can access the Session variables, but i cannot connect to Parse anymore. HELP ! (-; (i will change my question)
0

On the start of your user logged in, you can do something like

session_start();
$_SESSION['USER'] = <some user info>;

In your other pages you can see if

if(isset($_SESSION['USER'])){
  // do something

}

at last on logout

session_destroy();

will kill the session

1 Comment

Thanks!! If i understand it correctly, this solutions transfers user data in a variable to another php file. But suppose this other php file needs to get something out of the database. Does it then have to log itself in again? And is it then logged in twice...?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.