Jump to content


Photo

Sessions And Header Problem


  • Please log in to reply
5 replies to this topic

#1 Malcolmhire2001

Malcolmhire2001
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 02 May 2006 - 02:10 PM

Hello i'm i'm busy building a website using Tables, i am trying to create a login with sessions but i get the error:


[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Server\Apache2\htdocs\Rockcliff Rugby Club\index.php:13) in C:\Server\Apache2\htdocs\Rockcliff Rugby Club\logged_in.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Server\Apache2\htdocs\Rockcliff Rugby Club\index.php:13) in C:\Server\Apache2\htdocs\Rockcliff Rugby Club\logged_in.php on line 2
Welcome hirem You have successfully logged in [/quote]

this error is when you login!

when i try to go to a page which requires me to be in a session and i am not i a session i get this error:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Server\Apache2\htdocs\Rockcliff Rugby Club\index.php:13) in C:\Server\Apache2\htdocs\Rockcliff Rugby Club\bookings.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Server\Apache2\htdocs\Rockcliff Rugby Club\index.php:13) in C:\Server\Apache2\htdocs\Rockcliff Rugby Club\bookings.php on line 2

Notice: Undefined index: admin_ID in C:\Server\Apache2\htdocs\Rockcliff Rugby Club\bookings.php on line 3

Warning: Cannot modify header information - headers already sent by (output started at C:\Server\Apache2\htdocs\Rockcliff Rugby Club\index.php:13) in C:\Server\Apache2\htdocs\Rockcliff Rugby Club\bookings.php on line 5
[/quote]

these are my pages i am using:


index.php
<?php
if (!isset($_GET['page'])) 
{
        $url="index.php?page=home";
        header("Location: /Rockcliff Rugby Club/".$url);
        exit;
}
else
{
$page = $_GET['page'];
}
?>
<style type="text/css">
<!--
body {
    background-color: #EFEBDE;
}
.style1 {
    font-size: 24px;
    font-family: Verdana, Arial, Helvetica, sans-serif;
}
-->
</style>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_nbGroup(event, grpName) { //v6.0
  var i,img,nbArr,args=MM_nbGroup.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
        if (!img.MM_up) img.MM_up = img.src;
        img.src = img.MM_dn = args[i+1];
        nbArr[nbArr.length] = img;
    } }
  } else if (event == "over") {
    document.MM_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
      nbArr[nbArr.length] = img;
    }
  } else if (event == "out" ) {
    for (i=0; i < document.MM_nbOver.length; i++) {
      img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  } else if (event == "down") {
    nbArr = document[grpName];
    if (nbArr)
      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
      nbArr[nbArr.length] = img;
  } }
}
//-->
</script>
<body onLoad="MM_preloadImages('buttons/Login_over.gif','buttons/bookings_over.gif','buttons/aboutus_over.gif','buttons/contactus_over.gif','buttons/members_over.gif')">
<table width="800" height="613" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="69"><div align="center" class="style1"><img src="buttons/title.gif" width="800" height="94"></div></td>
  </tr>
  <tr>
    <td height="28" bgcolor="#D6CFA5"><div align="center">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr bordercolor="#000000" bgcolor="#D1CFA7">
            <td><a href="?page=login" target="_top" onClick="MM_nbGroup('down','group1','login','buttons/Login_over.gif',1)" onMouseOver="MM_nbGroup('over','login','buttons/Login_over.gif','',1)" onMouseOut="MM_nbGroup('out')"><img src="buttons/Login_up.gif" alt="Login" name="login" border="0" onload=""></a></td>
            <td><a href="?page=members" target="_top" onClick="MM_nbGroup('down','group1','members','buttons/members_over.gif',1)" onMouseOver="MM_nbGroup('over','members','buttons/members_over.gif','',1)" onMouseOut="MM_nbGroup('out')"><img src="buttons/members_up.gif" alt="Register" name="members" border="0" onload=""></a></td>
            <td><a href="?page=bookings" target="_top" onClick="MM_nbGroup('down','group1','bookings','buttons/bookings_over.gif',1)" onMouseOver="MM_nbGroup('over','bookings','buttons/bookings_over.gif','',1)" onMouseOut="MM_nbGroup('out')"><img name="bookings" src="buttons/bookings_up.gif" border="0" alt="Bookings" onload=""></a></td>
            <td><a href="?page=about_us" target="_top" onClick="MM_nbGroup('down','group1','about_us','buttons/aboutus_over.gif',1)" onMouseOver="MM_nbGroup('over','about_us','buttons/aboutus_over.gif','',1)" onMouseOut="MM_nbGroup('out')"><img name="about_us" src="buttons/aboutus_up.gif" border="0" alt="About Us" onload=""></a></td>
            <td><a href="?page=contact_us" target="_top" onClick="MM_nbGroup('down','group1','Contact_us','buttons/contactus_over.gif',1)" onMouseOver="MM_nbGroup('over','Contact_us','buttons/contactus_over.gif','',1)" onMouseOut="MM_nbGroup('out')"><img name="Contact_us" src="buttons/contactus_up.gif" border="0" alt="Contact Us" onload=""></a></td>
          </tr>
        </table>
    </div></td>
  </tr>
  <tr>
    <td valign="top">
<?php
if(file_exists($page.'.php'))
{ 
  include $page.'.php'; 
}
else
{
echo "No Page Exists";
}
?>
    </td>
  </tr>
</table>

login_php.php
<?php
session_start();

include("connection.inc.php");
$connection = connect();

$admin_ID = $_POST['admin_ID'];
$password = $_POST['password'];

if ((!$admin_ID) || (!$password))
    {
    header("Location: index.php?page=login");
    exit;
}


$sql = "SELECT * FROM tbl_admin_members WHERE admin_ID = '$admin_ID' AND password = '$password'";
$result = @mysql_query($sql) or die ("Unable to run query");
$count = mysql_num_rows($result);
    if ($count !=0)
    {
        $_SESSION['admin_ID'] = $admin_ID;
        header ("Location: index.php?page=logged_in");
    }
    else
    {
        $msg = "<p>Your username or password was invalid, 
        please click the hyperlink below and try again <br><a href= index.php?page=login>Login</a></br></p>";
    }
?>


<html>

<head>
  <title>Logged In</title>
</head>

<body>
<?php
 echo "$msg";
?>

</body>

</html>

bookings.php

<?php 
    session_start();
if (!$_SESSION['admin_ID'])
    {
        header("Location: index.php?page=login");
        exit;
    }
    else
    {
        $admin_ID = $_SESSION['admin_ID'];
    }
?>
Bookings




If someone could please help i would be very greatful


thanks, Malcolm


#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 02 May 2006 - 02:37 PM

There is a sticky thread at the top of the newbies forum that should solve your problem.

#3 Malcolmhire2001

Malcolmhire2001
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 02 May 2006 - 03:00 PM

i have tried that tread but everything within my code seems fine. also the code for the session is placed at the top of every session page like it says in the tread

thanks, malcolm

#4 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 02 May 2006 - 06:41 PM

Also, look at the error reporting level in the php.ini file on your server.

Change to this:

error_reporting = E_ALL ^ E_NOTICE

What you're getting, is a Warning notice, and not an error. Depending on your code, you'll probably also be getting annoying notices.

Wait, are you using Dreamweaver generated code? Leapiin' lizards, batman. There be your first mistake. Take a little time to learn the code, and you will be a lot better off.
PHP Ninja

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 May 2006 - 11:23 AM

Ok. I see your problem as you are including your pages at the bottom of index.php and in the files you are including has sessin_start(). Now you cant use session_start when you have outputed something to the browser ie text/html. In index.php you have html being outputted which iso n line 13 and so you get the warning message about headers already sent.

To resolve the issue take out session_start() from any files you are including into index.php and place session_start as the first line in index.php. So the top part of index.php should look like this and any other files being included must have session_start striped out of them:
<?php
session_start();

if (!isset($_GET['page']))
{
        $url="index.php?page=home";
        header("Location: /Rockcliff Rugby Club/".$url);
        exit;
}


#6 Malcolmhire2001

Malcolmhire2001
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 03 May 2006 - 12:11 PM

thanks for your help

i'v tried the Caesar solution and that make the warning errors disapper but it still isn't wat i want the site to do.

i have also tried wildteen88 solution and that works to some extent i now have these errors when clicking on a page that needs you to login to view the page.
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
Notice: Undefined index: admin_ID in C:\Server\Apache2\htdocs\Rockcliff Rugby Club\bookings.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at C:\Server\Apache2\htdocs\Rockcliff Rugby Club\index.php:15) in C:\Server\Apache2\htdocs\Rockcliff Rugby Club\bookings.php on line 4
[/quote]

this is supposed to take you back to the login page because you are not logged in

the code used for this page
<?php 
if (!$_SESSION['admin_ID'])
    {
        header("Location: index.php?page=login");
        exit;
    }
    else
    {
        $admin_ID = $_SESSION['admin_ID'];
    }
?>
Bookings


the login form is workin fine now without any errors thanks for your help

if you could help me with this last problem it would be great.

thank you, Malcolm




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users