Note that there are some explanatory texts on larger screens.

plurals
  1. POheaders already sent while starting session
    text
    copied!<p>When running my php code the following errors appear:</p> <pre><code> Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/chatvik/public_html/control.php:2) in /home/chatvik/public_html/control.php on line 5 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/chatvik/public_html/control.php:2) in /home/chatvik/public_html/control.php on line 5 </code></pre> <p>What's wrong and how to fix it? Here is the code, i have deleted the real db usernames and passwords for sequrity reasons, thanks for your help:</p> <pre><code>&lt;?php /*** begin our session ***/ session_start(); /*** check if the users is already logged in ***/ if(isset( $_SESSION['user_id'] )) { $message = 'Users is already logged in'; } /*** check that both the username, password have been submitted ***/ if(!isset( $_POST['username'], $_POST['password'])) { $message = 'Please enter a valid username and password'; } /*** check the username is the correct length ***/ elseif (strlen( $_POST['username']) &gt; 20 || strlen($_POST['username']) &lt; 4) { $message = 'Incorrect Length for Username'; } /*** check the password is the correct length ***/ elseif (strlen( $_POST['password']) &gt; 6 || strlen($_POST['password']) &lt; 4) { $message = 'Incorrect Length for Password'; } /*** check the username has only alpha numeric characters ***/ elseif (ctype_alnum($_POST['username']) != true) { /*** if there is no match ***/ $message = "Username must be alpha numeric"; } /*** check the password has only alpha numeric characters ***/ elseif (ctype_alnum($_POST['password']) != true) { /*** if there is no match ***/ $message = "Password must be alpha numeric"; } else { /*** if we are here the data is valid and we can insert it into database ***/ $phpro_username = filter_var($_POST['username'], FILTER_SANITIZE_STRING); $phpro_password = filter_var($_POST['password'], FILTER_SANITIZE_STRING); /*** connect to database ***/ /*** mysql hostname ***/ $mysql_hostname = 'localhost'; /*** mysql username ***/ $mysql_username = 'xxxxxxx_usr'; /*** mysql password ***/ $mysql_password = 'xxxxxxx'; /*** database name ***/ $mysql_dbname = 'xxxxxxx_usr'; try { $dbh = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password); /*** $message = a message saying we have connected ***/ /*** set the error mode to excptions ***/ $dbh-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); /*** prepare the select statement ***/ $stmt = $dbh-&gt;prepare("SELECT phpro_user_id, phpro_username, phpro_password FROM phpro_users WHERE phpro_username = :phpro_username AND phpro_password = :phpro_password"); /*** bind the parameters ***/ $stmt-&gt;bindParam(':phpro_username', $phpro_username, PDO::PARAM_STR); $stmt-&gt;bindParam(':phpro_password', $phpro_password, PDO::PARAM_STR, 40); /*** execute the prepared statement ***/ $stmt-&gt;execute(); /*** check for a result ***/ $user_id = $stmt-&gt;fetchColumn(); /*** if we have no result then fail boat ***/ if($user_id == false) { $message = 'Login Failed'; } /*** if we do have a result, all is well ***/ else { /*** set the session user_id variable ***/ $_SESSION['user_id'] = $phpro_username; /*** tell the user we are logged in ***/ header('Location:sucsess.htm'); } } catch(Exception $e) { /*** if we are here, something has gone wrong with the database ***/ $message = 'We are unable to process your request. Please try again later"'; } } ?&gt; </code></pre> <p>Sorry for asking this. I'm directly BAD at php so i'm glad you are helping me Thanks for the help</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload