Jump to content

Recommended Posts

Okay, I am taking the advice of a moderator and going to stop using Dreamweaver as a crutch for writing PHP apps.  I have some questions about sessions and have been reading through some online tutorials, but have some questions that may seem simple stupid about sessions, but would appreciate some input.

 

Do you have to put this at the top of each page on the site when using sessions to keep the sessions going from page to page or do you just put it on a page when you are starting a session or pulling info back from a session?

 

<?php
session_start();
?>

Link to comment
https://forums.phpfreaks.com/topic/55405-session_start-and-using-sessions-in-php/
Share on other sites

Yes, you must put session_start() at the top of everypage that makes use of the $_SESSION superglobal array.

 

So therefore, if you are checking if someone is logged in, for instance, then every page will need session_start. If the page does nothing at all with sessions, you dont need it.

What i normally do is create a file (say session.php for arguments sake) and include that file in every file you need it... for example...

 

You would call the file when you have to pass session vairibles, but you wouldent call the file if you have a file that didnt need the sessions varibles...

 

normally what i do is this:

 


// some page we will call it members.php

require "session.php";

if ($_SESSION['user'] != $_COOKIE['user']) // some basic validation... needs tweekeing obviously.
{
   die("naughty person");
}
else
{
   //typicly function call
}

 

and then in the sessions fiel you would have somethign like this:

 


session_start();

if (!isset($_SESSION['user']))
{
   header("Location: index.php") // if the session dont exist we send them to the home page.
   die();
}
define("user",$_SESSION['user']);
// and then you continue to define constants untill all your session varibles are assigned... etc

 

:)

Kewl info.  So if I use session_start it has to be the first thing on the page on the first thing in an include and the first include on the page? 

 

Also, if the user is going to be going to pages that I don't use the session on in between pages that I do use the session on do I need to still put the session_start at the top of the other pages to keep the session going?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.