I've changed my hosting server from a Windows to a Linux system. But when I run my PHP program, I get this errors:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/content/p/y/c/francis/html/login/login.php:2) in /home/content/p/y/c/francis/html/login/login.php on line 4
and
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/p/y/c/francis/html/login/login.php:2) in /home/content/p/y/c/francis/html/login/login.php on line 4
This is the code of my program:
<?php
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
if ($username && $password)
{
$connect = mysql_connect(***,***,***);
mysql_select_db("phploginregister") or die("Couldn't find db");
$query = mysql_query("SELECT * FROM users WHERE username='$username'");
$numrows = mysql_num_rows($query);
if ($numrows != 0)
{
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
//check to see if they match!
if ($username == $dbusername && md5($password) == $dbpassword)
{
echo "You're in! <a href='member.php'>Click</a> here to enter the member page.";
$_SESSION['username'] = $dbusername;
}
else
echo "Incorrect password";
}
else
die("That user doens't exist!");
}
else
die("Please enter an username and password");
?>
What is wrong in the code, because it workend fine on a Windows host...
You get the error because there are some output before you have initiated session_start(); This could be caused because of your editor that include a BOM character in the beginning of your file. Try open the code in notepad and see if there are any lines before session_start(), (spaces) or things like that and remove them.
To fix your editor if it add a bom in your file, you need to go to your settings and turn it off.
You have a leading BOM, new line or other whitespace character before the opening <?php tag.
The errors talk about line 2 and line 4, but in the actual code above session_start() is called on line 3. Therefore, leading whitespace is the problem...
i think you should add
ob_start();
in the first of your code
and in the bottom add
ob_get_contents();
ob_end_flush();
because of
session send headers to server , also you added echo ( this also tell server its html with headers )
server now has to headers so use the ob_start(); and ob_end_flush(); to work :)
Related
I am trying to create a login form, I created the html form and wrote this PHP code but I keep on getting an error about session_start(); and header already sent
The error:
Warning: session_start(): Cannot send session cookie - headers already sent by
<?php
session_start();
if($_POST['submit'])
{
include_once('my_connection.php');
$username = strip_tags($_POST['username']);
$password = strip_tags($_POST['password']);
$sql = "SELECT id,username,password FROM users WHERE username = '$username' AND password = '$password'";
$query = mysqli_query($connect,$sql);
if($query) {
$row = mysqli_fetch_row($query);
$id = $row[0];
$dbusername = $row[1];
$dbpassword = $row[2];
if($username == $dbusername && $password == $dbpassword)
{
$_SESSION['username'] = $username;
$_SESSION['id'] =$id;
header('Location:home.php');
}else {
echo "Incorrect username or password.";
} } }?>
I put the session_start() on the top of the code but still don't work
Warning: session_start(): Cannot send session cookie
To start with, always have your start_session() at the top. Yes, adding ob_start() above it may fix it and adding ob_flush() at the bottom could but it looks like it hasn't so:
If you're requiring the same file or another file, ensure the file you're requiring does not have a session_start() function within it. Ensure you have no duplicates of session_start() anywhere else either in the same code.
Also, a big reason actually may be because you're missing syntax in your header() and the program is not finishing/ending, only requiring.
Change it to this:
header('Location: home.php');
exit;
Sources to further help you:
Source 1
Source 2
I have created a php login form, which works fine using xampp & phpmyadmin,
but when I upload it to my server, I can register a user but get the following error when trying to log in.
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /php/index.php:9) in /php/login.php on line 24
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /php/index.php:9) in /php/login.php on line 24
Warning: Cannot modify header information - headers already sent by (output started at /php/index.php:9) in /php/login.php on line 28
i have php included the login form on the index.php page
the login.php code is...
<?php include("dbconnect.php"); ?>
<?php
if(isset($_POST["submit1"])){
if(!empty($_POST['user']) && !empty($_POST['pass'])) {
$user=$_POST['user'];
$pass=$_POST['pass'];
$query=mysql_query("SELECT * FROM login WHERE username='".$user."' AND password='".$pass."'");
$numrows=mysql_num_rows($query);
if($numrows!=0)
{
while($row=mysql_fetch_assoc($query))
{
$dbusername=$row['username'];
$dbpassword=$row['password'];
}
if($user == $dbusername && $pass == $dbpassword)
{
session_start();
$_SESSION['sess_user']=$user;
/* Redirect browser */
header("Location: member.php");
}
} else {
echo "Invalid username or password!";
}
} else {
echo "All fields are required!";
}
}
?>
Thank you
use ob_start() - Turn on output buffering
This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.
session_start(); should be first line of your code file.
<?php
if(isset($_POST["submit1"])){
session_start();
include("dbconnect.php");
if(!empty($_POST['user']) && !empty($_POST['pass'])) {
$user=$_POST['user'];
$pass=$_POST['pass'];
$query=mysql_query("SELECT * FROM login WHERE username='".$user."' AND password='".$pass."'");
$numrows=mysql_num_rows($query);
if($numrows!=0)
{
while($row=mysql_fetch_assoc($query))
{
$dbusername=$row['username'];
$dbpassword=$row['password'];
}
if($user == $dbusername && $pass == $dbpassword)
{
$_SESSION['sess_user']=$user;
/* Redirect browser */
header("Location: member.php");
}
} else {
echo "Invalid username or password!";
}
} else {
echo "All fields are required!";
}
}
I am working on a new project www.merapalwal.com. I have created a user login panel for this project where the user can login with their email id and password. Everything is working fine, user created and updated correctly. I used login form in header page which is included in all other pages.
I have created two db files, db-user.php (for db config) and db-sess.php (for db config with session_start). But if I use db-sess.php on the login form in header-top.php, it gives me the error:
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/mypalwal/public_html/index.php:16) in /home/mypalwal/public_html/includes/db-sess.php on line 3
When I use db-user.php, it allows a user to log in but does not show the username after welcome, please advise me code as under:
header.php:
<?php
ini_set('session.bug_compat_42',0);
ini_set('session.bug_compat_warn',0);
include('includes/db-sess.php');
$error = '';
$form = $_POST['login'];
$lemail = $_POST['lemail'];
$pass = $_POST['pass'];
$_SESSION['lemail'] = $_POST['lemail'];
if( isset($form) ) {
if( isset($lemail) && isset($pass) && $lemail !== '' && $pass !== '' ) {
$sql = mysql_query("SELECT * FROM `userdata` WHERE email='$lemail' and pass='$pass' and type='Normal User';");
if( mysql_num_rows($sql) != 0 ) { //success
$_SESSION['logged-in'] = true;
print "<script type=\"text/javascript\">";
print "window.location.href = \"users/index.php\"";
print "</script>";
exit;
}
else { $error = "Login Detail Incorrect"; }
} else { $error = 'Login Detail Missing';}
}
?>
db-user.php:
<?php
$dbhost = 'localhost';
$dbuser = '######';
$dbpass = '######';
$db = '#######';
$conn = mysql_connect($dbhost, $dbuser, $dbpass );
mysql_select_db($db);
?>
If I use the above code for db config, it allows login and redirects to the next page but does not show user details but when I use db-sess.php which has session_start in it, the login form allows me to login and shows the username.
If I use db-sess.php, it shows an error on index.php page:
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/mypalwal/public_html/index.php:16) in /home/mypalwal/public_html/includes/db-sess.php on line 3
There is some space before in one of the 3 files, which causes the page output to be started and headers to be sent. Remove the space and especially remove the ?>
P.S.: You have 2 astonishing security issues in your code: SQL Injection possibilities and clear text passwords.
hello this is my first post on stackoverflow i am at beginner level at php, mysql and work on a php log in page connected to a mysql database which i did try to test through xampp and getting the following error message
Warning: include(../storescripts/connect_to_mysql.php): failed to open stream: No such
file or directory in C:\xampp\htdocs\myonlinestore\storeadmin\admin_login.php
on line 15
Warning: include(): Failed opening '../storescripts/connect_to_mysql.php' for
inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\myonlinestore
\storeadmin\admin_login.php on line 15
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in
C:\xampp\htdocs\myonlinestore\storeadmin\admin_login.php on line 18
That information is incorrect, try again Click Here
I was able to successfully connect to the mysql database through dreamwavercs6 on win7 64bit and created a user for the db as also i created a administrator with full privileges in the created admin table. With a successful log in it should direct you to a follow up page called index.php which is a second index page only for admins to choose tasks, located in a subfolder in the same directory. The home page index.php is located here C:\xampp\htdocs\myonlinestore\index.php\
the file with the script called admin_login.php is shown under here
<?php
session_start();
if (isset($_SESSION["manager"])){
header("location: index.php");
exit();
}
?>
<?php
if (isset($_POST["username"]) && isset($_POST["password"])){
$manager = preg_replace('#[^A-Za-z0-9]#i','',$_POST["username"]);
$password = preg_replace('#[^A-Za-z0-9]#i','',$_POST["password"]);
include "../storescripts/connect_to_mysql.php";
$sql = mysql_query("SELECT id FROM admin WHERE username='$manager' AND
password='$password' LIMIT 1");
$existCount = mysql_num_rows($sql);
if ($existCount == 1){
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
}
$_SESSION["id"] = $id;
$_SESSION["manager"] = $manager;
$_SESSION["password"] = $password;
header("location: index.php");
exit();
} else {
echo 'That information is incorrect, try again Click Here';
exit();
}
}
?>
the script connect_to_mysql.php under here
<?php
$db_host = "localhost";
$db_username = "user";
$db_pass = "user";
$db_name = "myonlinestore_db";
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to
mysql");
mysql_select_db("$db_name") or die ("no database");
?>
the script index.php which is the landing page where on successful login from admin_login should redirect you to, under here
<?php
session_start();
if(!isset($_SESSION["manager"])){
header("location: admin_login.php");
exit();
}
$managerID = preg_replace('#[^0-9]#i', '',$_SESSION["id"]);
$manager = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["manager"]);
$password = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["password"]);
include"../storescripts/connect_to_mysql.php";
$sql=mysql_query("SELECT*FROM admin WHERE id='$managerID' AND username='$manager' AND
password='$password' LIMIT 1"); // query the person
$existCount=mysql_num_rows($sql); // count the nums
if ($existCount==0){//evaluate the count
echo "Your login session data is not on record in the database";
exit();
}
?>
the problem is that i can not log in through my firefox browser and getting the error as mentioned at the top. All addons,extensions in my firefox browser are on disable and accepting cookies is selected, can anyone help to fix this problem?
Many thanks in advance
Here in your script you are not being able to include your connect_to_mysql.php file.
include "../storescripts/connect_to_mysql.php";
You are trying to provide relative path. Make sure you are properly able to access that file from relative path you are including. i.e. your admin_login.php file.
You can try passing absolute path in your include:
include "C:\xampp\htdocs\myonlinestore\storescripts\connect_to_mysql.php";
please check path in your file manager if it is correct absolute path.
Remove one of the dots from the include("../if the scripts are in
C:\xampp\htdocs\myonlinestore\storescript.
From
C:\xampp\htdocs\myonlinestore\storeadmin\admin_login.php
"../" will take you to htdocs, so you need "./" to get to htdocs\myonlinestore
may be it will help you.
<?php
$server = "localhost";
$connection = mysqli_connect ($server,"root","","your_database_name");
if(!$connection){
// if not connected it will gives the error here
echo "server not found".mysql_error();
}
else{
echo "Connected";
}
?>
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Warning: Cannot modify header information - headers already sent
Headers already sent by PHP
Error: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\GameHutWebsite\data\connection.php:1) in C:\xampp\htdocs\GameHutWebsite\login.php on line 21
I am getting this error when the user enters the correct username and password in the login screen to send the user in the home page. This is the code:
<?php
if ($_POST)
{
$dsLogin = $dataobj->validateLogin($_POST["txtUsername"],$_POST["txtPassword"]);
if (mysql_num_rows($dsLogin))
{
$dsUserDetails = mysql_fetch_array($dsLogin);
$_SESSION["UserEmail"] = $dsUserDetails["UserEmail"];
$_SESSION["Username"] = $dsUserDetails["Username"];
$_SESSION["UserTypeId"] = $dsUserDetails["UserTypeId"];
header ("Location: index.php");
}
else
{
$msg = "Invalid Username and Password!";
}
}
?>
Connection Class:
<?php
class connection
{
function connect($sql)
{
$server = 'localhost';
$myDB = 'gamehutdb';
//connection to the database
$dbhandle = mysql_connect($server, 'root', "")
or die("Couldn't connect to SQL Server $server");
//select a database to work with
$selected = mysql_select_db($myDB)
or die("Couldn't open database $myDB");
//execute the SQL query and return records
$result = mysql_query($sql);
//return result set
return $result;
//close the connection
mysql_close($dbhandle);
}
}
?>
You have used
header ("Location: index.php");
AFTER sending something to the client (ie, using echo or print), make sure that there is nothing sent to the client before that code.
Another reason might be a BOM problem.
This is probably due to the fact that your connection class is emitting some kind of output
Comment out the "header" line and run your script. If you get some output, then that's the reason.
You should conditionally launch the "header" line based on whether you get an error or not, at least while you troubleshoot your problem.