Jump to content

How to start/keep sessions correctly


Waliser

Recommended Posts

Hello all,

I have tried to fix this many a time before but never managed it. However now it is really annoying me as it clogs up my error file.

<?php

session_start();

session_register('sid');

if (isset($_SESSION['sid'])) {

} else {

  $tmp = md5(microtime());

  $_SESSION['sid']=$tmp;

}

$db_server = "server";

$db_name = "name";

$db_user = "user";

$db_passwort = "pw";

$db = @MYSQL_CONNECT($db_server,$db_user,$db_passwort) or die ("no connection please contact me");

$db_check = @MYSQL_SELECT_DB($db_name);

 

 

?>

This little file is called from every page on my website and is causing this line to appear lots

[11/Mar/2012:21:00:18 +0100] PHP Notice:  A session had already been started - ignoring session_start() in /home/www/EN/config/config.php on line 2

 

What is the correct way of setting the session i.e. how do I correctly start it at a given point and then keep it throughout?

From my understandin in the older php versions I could have just used session_register('sid'); and didn't need the start (but if I did that then had error) while now I have just read that the session_register('sid') has been removed as of 5.4.0.

 

Help would be muchly appreciated, I don't use php that often, just made my site work in the beginning and ignored this but it annoyed me the other day when I saw the size of the error file.

 

thank you

Waliser

Link to comment
Share on other sites

I did, but it might be that I am reading it wrongly.

My standard pages start

<?
include("config.php");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

The config is what I wrote in the prev post.

I don't see that I am outputting anything prior to the session. I am most likely a bit slow here sorry.

 

thanks

Link to comment
Share on other sites

... A session had already been started ...

 

It is possible that you are including a php file that is itself including config.php. If this is the case, you can either change config.php to not start a session if one has already been started, or change all of the include('config.php'); statements in all of your files to be include_once('config.php');

 

It is also possible that your php.ini is setup to automatically start a session for all requests. In this case, you can either disable this setting in the ini file, or (again) change the config.php to not start a session if one has already been started.

Link to comment
Share on other sites

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.