3 Replies - 297 Views - Last Post: 23 February 2013 - 02:23 AM Rate Topic: -----

#1 Silent_Symon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 22-February 12

IF statement for checking if a session needs to be started

Posted 22 February 2013 - 06:45 PM

Hello, I am in the process of building a website, I am currently working with sessions so that registered users will be able to interact with object on the site that would not be accessible to the general non-registered user.

A very simple example of this is in my index page I have

<?php
session_start();
if($_SESSION['username'])
{
        echo "<h3>Welcome ".$_SESSION['username'];
}
else

echo "<a href='RegLog.php'>Login or Register</a>"
?>



what I want is a way of determining if the user has logged in before the session is started, if they havn't logged in, then "Login or Register" will be displayed, if they have logged in, then "welcome $_SESSION['username'] will be displayed.

I suppose it is a simple if statement that is needed, however I can't for the life of me figure it out.

When I visit other pages (not index.php) it works fine as they already know that the user has logged in, its just the initial page that the user is redirected back to upon logging in that i'm having difficulty with!

Is This A Good Question/Topic? 0
  • +

Replies To: IF statement for checking if a session needs to be started

#2 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3718
  • View blog
  • Posts: 5,989
  • Joined: 08-June 10

Re: IF statement for checking if a session needs to be started

Posted 22 February 2013 - 07:28 PM

Moved to the PHP forum.

Quote

what I want is a way of determining if the user has logged in before the session is started

You mean before the session_start() function is called? Why?

If you really wanted this, though, I suppose you could use a cookie flag to tell you if a user has been logged in previously, and only start the session if said cookie is present. It's sort of a wasted effort, though. In a typically PHP application, you are likely to start a session in 99.999% of all requests for one reason or another.
Was This Post Helpful? 0
  • +
  • -

#3 Silent_Symon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 22-February 12

Re: IF statement for checking if a session needs to be started

Posted 23 February 2013 - 02:02 AM

the reason i am asking is because I get the following error message when opening the index page after clearing the cache / logging out


Warning: session_start() [
function.session-start
]: Cannot send session cookie - headers already sent by (output started at "location of file" - line in file that "Session-Start()" is found

I just presumed that it was because a user is no longer logged in and therefore was starting the session when no data was being sent to it.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3371
  • View blog
  • Posts: 11,420
  • Joined: 12-December 12

Re: IF statement for checking if a session needs to be started

Posted 23 February 2013 - 02:23 AM

This error is because you have some output before calling session_start(). It could be a DOCTYPE declaration, or even a space. Nothing must be output before the call to session_start().

The docs should be your first port of call.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1