Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write session start on 1 page or all pages?

Tags:

All the tutorials say to put session start. They don't say if that should be in all pages on the website, or some, or only 1.

And if it's only 1 page, does it have to be the main page? Or a page with a form that I am making that puts the session ID in the database? If the visitor never visits a page with a session id but they are on the site, do they still have a session id?

like image 346
hmwhat Avatar asked Aug 20 '11 03:08

hmwhat


People also ask

Should I use session start on every page?

It must be on every page you intend to use. The variables contained in the session—such as username and favorite color—are set with $_SESSION, a global variable. In this example, the session_start function is positioned after a non-printing comment but before any HTML.

Where do I put session start?

You want to put session_start(); at the top of your page before any other code. However, if you are using includes to make your life easier, it's best to put it at the very top of a file that is included in all files.

How do you write a session?

Before you can store any information in session variables, you must first start up the session. To begin a new session, simply call the PHP session_start() function. It will create a new session and generate a unique session ID for the user. The PHP code in the example below simply starts a new session.

What is the use of session start?

Description ¶ session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie. When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers.


1 Answers

You need to put this in each page that need to access the session data before accessing (or creating) any session data.

See: http://php.net/manual/en/function.session-start.php

like image 124
theprogrammer Avatar answered Oct 21 '22 09:10

theprogrammer