Author Topic: php sessions errors  (Read 598 times)

darrelvf

  • New Member
  • *
  • Posts: 8
  • Karma: 0
    • View Profile
I need help with the following error message

session_start(): Cannot send session cache limiter - headers already sent

I get the error at my website
http://www.v-tran.net/drivetech/ there is a test user named test with a password of trail

here is the script that sends this error


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  <?php
  session_start
();
  
  
//Include db settings and create a connection:
  
include("drive_tech_db_info.php");
  
  
//Create variable for username input and prevent sql injections:
  
$user_name $_POST['user_name'];
  
//Create variable for password input, prevent sql injections and hash it with md5:
 
$password $_POST['password'];
 
 
//Select matching username and password from admin table based on input:
 
$sql "SELECT * FROM Drivers WHERE user_name = '$user_name' AND password = '$password'";
 
//Execute query to db:
 
$execute mysqli_query($connect,$sql);
 
 
//If user input doesn't match a user in db:
 
if (mysqli_num_rows($execute) != && $_SERVER["REQUEST_METHOD"] == "POST") {
         
//Create error message:
     
$errormsg "<p><span class='errormsg'>The username and/or password you entered was incorrect!</span></p>";
 }
 
//Else if user exists in db:
 
elseif (mysqli_num_rows($execute) == 1) {
 
	

 
	
            while(
$row mysqli_fetch_array($execute)){
 
 
$Driver_ID_Number "" $row['Driver_ID_Number'] . "";
 
$Driver_First_Name "" $row['Driver_First_Name'] . "";
 
$Driver_Last_Name "" $row['Driver_Last_Name'] . "";
 }
 
     
//Set username session variable based on username input:
     
$_SESSION['user_name'] = $user_name;
     
$_SESSION['Driver_ID_Number'] = $Driver_ID_Number;
     
$_SESSION['Driver_First_Name'] = $Driver_First_Name;
     
$_SESSION['Driver_Last_Name'] = $Driver_Last_Name;
 }
 
//If user is already logged in, redirect to admin page:
 
if (!isset($_SESSION['user_name'])) {
     
header('Location: welcome.php');
 }
 
?>

« Last Edit: March 21, 2017, 06:53:40 pm by astonecipher »

PHP Help Forum

php sessions errors
« on: March 21, 2017, 06:52:26 pm »


astonecipher

  • Software Engineer
  • Professional PHP Helper
  • Senior Member
  • *
  • Posts: 2435
  • Karma: 95
    • View Profile
Questions? Go here first.
http://www.phptherightway.com/

PHP Help Forum

Re: php sessions errors
« Reply #1 on: March 21, 2017, 06:54:38 pm »



PHP Help Forum

Re: php sessions errors
« Reply #2 on: March 21, 2017, 07:04:48 pm »



PHP Help Forum

Re: php sessions errors
« Reply #3 on: March 21, 2017, 07:15:53 pm »



PHP Help Forum

Re: php sessions errors
« Reply #4 on: March 21, 2017, 07:20:35 pm »



PHP Help Forum

Re: php sessions errors
« Reply #5 on: March 21, 2017, 11:20:45 pm »


darrelvf

  • New Member
  • *
  • Posts: 8
  • Karma: 0
    • View Profile
wow where is the love the code calls includes the contain the variables

the entire code reads

<?php
session_start();

//Include db settings and create a connection:
include("../includes/ride_request_db_info.php");

$connect = mysqli_connect("$servername", "$db_username", "$db_password", "$db_name") or DIE('Connection to host failed, perhaps the service is down!');

//Create variable for username input and prevent sql injections:
$user_name = $_POST['user_name'];
//Create variable for password input, prevent sql injections and hash it with md5:
$password = $_POST['password'];

//Select matching username and password from admin table based on input:
$sql = "SELECT * FROM Drivers WHERE user_name = '$user_name' AND password = '$password'";
//Execute query to db:
$execute = mysqli_query($connect,$sql);

//If user input doesn't match a user in db:
if (mysqli_num_rows($execute) != 1 && $_SERVER["REQUEST_METHOD"] == "POST") {
        //Create error message:
    $errormsg = "<p><span class='errormsg'>The username and/or password you entered was incorrect!</span></p>";
}
//Else if user exists in db:
elseif (mysqli_num_rows($execute) == 1) {
   
               while($row = mysqli_fetch_array($execute)){

$Driver_ID_Number = "" . $row['Driver_ID_Number'] . "";
$Driver_First_Name = "" . $row['Driver_First_Name'] . "";
$Driver_Last_Name = "" . $row['Driver_Last_Name'] . "";
}

    //Set username session variable based on username input:
    $_SESSION['user_name'] = $user_name;
    $_SESSION['Driver_ID_Number'] = $Driver_ID_Number;
    $_SESSION['Driver_First_Name'] = $Driver_First_Name;
    $_SESSION['Driver_Last_Name'] = $Driver_Last_Name;
}
//If user is already logged in, redirect to admin page:
if ((!isset($_SESSION['user_name'])) && (isset($_SESSION['Driver_ID_Number'])) && (isset($_SESSION['Driver_First_Name'])) && (isset($_SESSION['Driver_Last_Name']))) {
    header('Location: welcome.php');
}
?>
?>

<!DOCTYPE html>

<html>

<head>

<title>V-Tran's Drive-Tech</title>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<link href="css/style.css" rel="stylesheet">

</head>

<body>

<div id="container">

<?php
echo $errormsg;
?>

<h2>Login</h2>

<form method="post" action="do_login.php">
<?php
echo "<input type=\"text\" size=\"25\" name=\"user_name\" value=\"\">";
?>

<br>
<label>Password:</label>

<br>
<input type="password" size="25" name="password" value="">

<br><br>
<input type="submit" value="Login">
</form>

</div>

</body>

</html>

PHP Help Forum

Re: php sessions errors
« Reply #6 on: March 21, 2017, 11:27:25 pm »



PHP Help Forum

Re: php sessions errors
« Reply #7 on: March 21, 2017, 11:33:45 pm »


Kevin Rubio

  • Professional PHP Helper
  • Senior Member
  • *
  • Posts: 1813
  • Karma: 108
  • Programmer Available for hire
    • View Profile
    • Galaxy Internet
PDO Bumpstart Database

The XY Problem
The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help. http://xyproblem.info/

PHP Help Forum

Re: php sessions errors
« Reply #8 on: March 21, 2017, 11:50:37 pm »



darrelvf

  • New Member
  • *
  • Posts: 8
  • Karma: 0
    • View Profile
here is the whole script before it was edited to fit my needs I need mysqli as my server uses PHP Version    5.4.45
MySQL Version    5.5.51-38.2 the script I download used an earlier version of php and my_sql

Script 1 named adminpage.php

<?php
//Resume existing session:
session_start();
//If user isn't logged in, redirect to login page:
if (!isset($_SESSION['user'])) {
    header('Location: login.php');
}
?>
<!DOCTYPE html>
<!--
This login script was fetched from
http://annice.se and is created by Annice Strömberg, 2015.
-->
<html>
    <head>
        <title>Login 1.0</title>

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <link href="css/style.css" rel="stylesheet">
    </head>
    <body>

        <div id="container">

            <h2>Welcome <?php echo $_SESSION['user']; ?></h2>

            <p>You are now logged in!</p>

            <p>[ <a href="includes/logout.inc.php">Log out</a> ]</p>

        </div>
       
    </body>
</html>

Script 2 named login.php

<?php
include ("includes/loginproc.inc.php");
?>
<!DOCTYPE html>
<!--
This login script was fetched from http://annice.se and is created by Annice Strömberg, 2015.
-->
<html>
    <head>
        <title>Login 1.0</title>

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <link href="css/style.css" rel="stylesheet">
    </head>
    <body>

        <div id="container">

            <?php echo $errormsg; ?>

            <h2>Login</h2>

            <form method="post" action="login.php">
                <label>Username:</label><br>
                <input type="text" size="25" name="usern" value=""><br>
                <label>Password:</label><br>
                <input type="password" size="25" name="pass" value=""><br>
                <input type="submit" value="Login">
            </form>

        </div>

    </body>
</html>

Script 3 named config.inc.php

<?php

$hostname = 'localhost'; //Your MySQL hostname (usually named as 'localhost').
$dbname   = 'dbname'; //Your database name.
$username = 'dbuser'; //Your database username.
$password = ''; //Your database password (if your database has no password, leave it empty).

//Let's connect to host:
mysql_connect($hostname, $username, $password) or DIE('Connection to host failed, perhaps the service is down!');
//Select the database:
mysql_select_db($dbname) or DIE('Database name is not available!');

?>

Script 4 named loginproc.inc.php

<?php
//Start session:
session_start();

//Include db settings and create a connection:
include("config.inc.php");

//Create variable for username input and prevent sql injections:
$username = mysql_real_escape_string($_POST['usern']);
//Create variable for password input, prevent sql injections and hash it with md5:
$password = mysql_real_escape_string(md5($_POST['pass']));

//Select matching username and password from admin table based on input:
$sql = "SELECT * FROM admin WHERE username = '$username' AND password = '$password'";
//Execute query to db:
$execute = mysql_query($sql);

//If user input doesn't match a user in db:
if (mysql_num_rows($execute) != 1 && $_SERVER["REQUEST_METHOD"] == "POST") {
    //Create error message:
    $errormsg = "<p><span class='errormsg'>The username and/or password you entered was incorrect!</span></p>";
}
//Else if user exists in db:
else if (mysql_num_rows($execute) == 1) {
    //Set username session variable based on username input:
    $_SESSION['user'] = $username;
}
//If user is already logged in, redirect to admin page:
if (isset($_SESSION['user'])) {
    header('Location: adminpage.php');
}

Script 5 named logout.inc.php

<?php

//Resume existing session:
session_start();

//Destroy current session:
unset($_SESSION['user']);

//Redirect to logged out page:
header('Location: ../loggedout.html');
?>

and 1 html page named loggedout.html

<!DOCTYPE html>
<!--
This login script was fetched from http://annice.se and is created by Annice Strömberg, 2015.
-->
<html>
    <head>
        <title>Login 1.0</title>

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <link href="css/style.css" rel="stylesheet">
    </head>
    <body>

        <div id="container">
           
            <h2>Logged out page</h2>

            <p>This is just a HTML page which is not password-protected.</p>

            <p>[ <a href="login.php">Login</a> ]</p>

        </div>

    </body>
</html>