Hit Counter Code giving ERROR

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

vishalonne
New php-forum User
New php-forum User
Posts: 11
Joined: Fri Jul 27, 2012 9:52 am

Hit Counter Code giving ERROR

Postby vishalonne » Fri Aug 03, 2012 12:39 am

Hi All
I am getting an warning on my web page when I uploaded the page on server I just writing a PHP Page Hit Counter-

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/cbsecpsn/public_html/csnip/form_453570.php:7) in /home/cbsecpsn/public_html/csnip/counter.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/cbsecpsn/public_html/csnip/form_453570.php:7) in /home/cbsecpsn/public_html/csnip/counter.php on line 2

This the output - Total page views = 1
Here is the Code -
Counter.php

Code: Select all

<?PHP
session_start();
if(isset($_SESSION['views'])){
$_SESSION['views'] = $_SESSION['views']+ 1;
}else{
$_SESSION['views'] = 1;
}
echo "Total page views = ". $_SESSION['views'];
?>


And I used it like this in Survey_Form.php

Code: Select all

<div class='sfm_cr_box' style='padding:3px; width:350px'>
<?php
echo "<hr><div align=\"center\">";
include_once "counter.php"; // this will include the counter.
echo "</div>";
?>
</div>

What is the problem with the code

faust
New php-forum User
New php-forum User
Posts: 109
Joined: Thu May 03, 2012 7:22 am

Re: Hit Counter Code giving ERROR

Postby faust » Fri Aug 03, 2012 3:31 am

Code: Select all

<div class='sfm_cr_box' style='padding:3px; width:350px'>

this is your problem. You start session_start(); ALWAYS at the beginning of file. No output before it, not even a single whitespace or space.
Basically, this goes at the top of your file before all html output:

Code: Select all

include_once "counter.php"; // this will include the counter.

vishalonne
New php-forum User
New php-forum User
Posts: 11
Joined: Fri Jul 27, 2012 9:52 am

Re: Hit Counter Code giving ERROR

Postby vishalonne » Fri Aug 03, 2012 5:02 am

Thank you for looking in my problem, actually I want to display hit counter at the bottom of the page. Do I need session...?????
I just want to keep track of numbers of visitors.

Is it possible without session??

kyle04
New php-forum User
New php-forum User
Posts: 94
Joined: Sat Jul 07, 2012 1:36 pm

Re: Hit Counter Code giving ERROR

Postby kyle04 » Fri Aug 03, 2012 5:56 am

You just need to re-arrange your scripts a little :

Counter.php

Code: Select all

<?
if(isset($_SESSION['views'])){
$_SESSION['views'] = $_SESSION['views']+ 1;
}else{
$_SESSION['views'] = 1;
}
?>


Survey_Form.php

Code: Select all

<?PHP
session_start();
include_once ("counter.php");

echo "<div class='sfm_cr_box' style='padding:3px; width:350px'>";
echo "<hr><div align=\"center\">";
echo "Total page views = ". $_SESSION['views'];
echo "</div>";
echo "</div>";

?>

santbirsingh
New php-forum User
New php-forum User
Posts: 1
Joined: Tue Sep 10, 2013 9:37 pm

Re: Hit Counter Code giving ERROR

Postby santbirsingh » Tue Sep 10, 2013 10:06 pm

I am also getting the same error while executing this script name a_login.php

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/lawnmowe/public_html/a_login.php:5) in /home/lawnmowe/public_html/a_login.php on line 31

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/lawnmowe/public_html/a_login.php:5) in /home/lawnmowe/public_html/a_login.php on line 31

Warning: Cannot modify header information - headers already sent by (output started at /home/lawnmowe/public_html/a_login.php:5) in /home/lawnmowe/public_html/a_login.php on line 37

Please help me what is a issue with this script..

Code: Select all

<!DOCTYPE html>
<html>
<head>

<?php
 include 'config.php';
if ( isset( $_POST['Submit1'] ) ) { 
 $con=mysqli_connect("localhost","lawnmowe_crew1","9996057221","lawnmowe_nzis9_crewcut");
// $con=mysqli_connect($host,$user,$pass,$DB_name);
 if (mysqli_connect_errno($con))
   {
   //echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
   else
   {
   //echo "";
   }
 $user=$_POST['user'];
 $pass=$_POST['psw']; 
 $sql= "SELECT * FROM administrator WHERE a_user ='$user' and a_pass='$pass'";
 //echo $sql;
 $result = mysqli_query($con,$sql);
 $count =0;
 while($row = mysqli_fetch_array($result))
   {
   $count=1;   
   $us=$row['a_user'];
   $ps=$row['a_pass'];   
   }   
    if($count==1){
session_start();
$_SESSION['user']=$us;
$_SESSION['auth']='TRUE';
//session_register("myusername");
//session_register("mypassword");
//echo "Login Successfull Redirecting...";
header("location: control/controlpanel.php");
 }
 else {
 echo "<div  style='border: solid 1px #CCCCCC;
        border-radius: 5px;
   background-color: #f1f6e3;
        color:grey;
        font-family:arial, Helvetica, serif;
        font-size:14px;
   padding: 3px;
        height:15px;
   margin: 4px 4px 0px 4px;
   min-width:350px;'>Invalid Username or Password </div><br/>";
 }
mysqli_close($con);
 }   
?>
<style>
#con
{
width:500px;
height:300px;
border-radius:20px;


background-color:#455d28;
box-shadow: 0px 0px 15px black;
color:white;
text-align=left;
}

#btn
{
width:100px;
height:30px;
border-radius:5px;
border-style:none;
background-color:#5f7e3a;
box-shadow: 0px 0px 2px grey;
color:white;
text-align=left;
}

#btn:hover
{
width:100px;
height:30px;
border-radius:5px;
border-style:none;
background-color:#526f2f;
box-shadow: 0px 0px 2px black;
color:white;
text-align=left;
}
#in
{
font-family:arial;
font-weight:bold;
color:#acc491;
}
</style>
</head>
<body bgcolor=#5f7e3a>
<br/><br/><br/><br/>
<div align='center'>
<div id='con'>
<br/>

<p style='font-family:tahoma; font-size:20px; color:grey;'> Administrator | Login</>
<form action='a_login.php' method='POST'>
<table cellspacing='10'>
<tr><td> Login ID:</td><td><input id='in' Placeholder='Login ID' type='text' name='user'></td></tr>
<tr><td>Password:</td><td><input id='in' Placeholder='Password' type='password' name='psw'></td></tr>
<tr><td></td><td  align='left'><input type='checkbox' name='chkrmb' value='rmb'> Remember User name</td></tr>
<tr><td></td><td align='left'><input id='btn' type='submit' name='Submit1' value='Login'></td></tr>

</table>
</form>
</div></div>

</body>
</html>


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 4 guests