hanspeare_1 0 Light Poster

Hi there,

i have recieved this notice

Warning: Header may not contain more than a single header, new line detected in C:\xampp\htdocs\a\Student_Edit_Handler.php on line 53

and these are my codes. Kindly help me to fix this.

<?php
    session_start();
    $session_id = $_SESSION['user_id'];
    if($session_id == null){
       header("location:Student_Edit.php");
       die();
    }
    include 'Connect.php';
    $flag = "success";
    function rollbackData(){
        mysql_query(" ROLLBACK ");
        global $flag; 
        $flag = "error";
        if(mysql_error() != null){
            die(mysql_error());
        }
    }
    $student_id = $_POST['student_id'];
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $gender = $_POST['gender'];
    $date_of_birth = date("Y-m-d",strtotime($_POST['date_of_birth']));
    $contact_no = $_POST['contact_no'];
    $grade = $_POST['grade'];
    $section = $_POST['section'];
    $LRN = $_POST['LRN'];
    $email1 = $_POST['email1'];
    $email2 = $_POST['email2'];
    $address = $_POST['address'];
    $description = $_POST['description'];
    $imagename = "";
    $flag= "";
        mysql_query("SET AUTOCOMMIT = 0 ");
        if(mysql_error() != null){
            die(mysql_error());
        }
    $query = "UPDATE student_information SET first_name='$first_name',last_name='$last_name',";    
    $query .= "gender='$gender',date_of_birth='$date_of_birth',contact_no='$contact_no',grade='$grade',section='$section',";
    $query .= "LRN='$LRN',email1='$email1',email2='$email2',address='$address',description='$description'";     
    $query .= " WHERE student_id='{$_SESSION['user_id']}'";      
    $result = mysql_query($query, $link_id);
    if(mysql_error() != null){
        die(mysql_error());
    }
    if($result)
    { 
        $flag = "success";
    }
    else
    {
         $flag = "error"; 
    }
    header("location:Student_Edit.php?flag=$flag&student_id=student_id=$student_id");
?>

Please advise

Dani AI

Generated

— short, practical diagnosis and fixes.

That warning means the string you passed to PHP’s header() contains a newline character (CR or LF). PHP blocks multiline header values to prevent HTTP response splitting / header injection, so any CR/LF in variables used inside a Location header will trigger that message. (stackoverflow.com)

Most likely problems in the posted code:

  • An HTML entity (&amp;) or a duplicated parameter in your redirect URL (badly formed query string).
  • Untrusted POST data (for example $_POST['student_id']) containing \r or \n.
    Fix: remove CR/LF from values and build the query safely (http_build_query or urlencode). Example pattern to use:
$student_id = str_replace(array("\r","\n"), '', trim($_POST['student_id']));
$flag = str_replace(array("\r","\n"), '', trim($flag));

$params = ['flag' => $flag, 'student_id' => $student_id];
header('Location: Student_Edit.php?' . http_build_query($params));
exit;

Use http_build_query() or urlencode() so special characters are encoded and you don’t accidentally inject newlines. (php.net)

Troubleshooting checklist:

  • var_dump the redirect variables to confirm no control characters; test preg_match('/[\r\n]/', $var).
  • Check included files (Connect.php) for stray output or a BOM before <?php — headers are sent when the first byte of output appears. Use output buffering (ob_start()) temporarily while you hunt whitespace. (php.net)

One more note: your code uses the old mysql_* functions; those were deprecated and removed in modern PHP. Migrate to mysqli or PDO and use prepared statements for security and future compatibility. (php.net)

References: PHP header() manual (see notes on headers/output), http_build_query() docs, urlencode() docs, CRLF/HTTP injection overview, mysql extension deprecation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.