Question: i need a simple php login code with session? Warning: session_register(): Cannot send session cookie - headers already sent by (output started at c:&#92;login.php:2) in c:&#92;login.php on line 20.\nAbove error occur when i use session.\nline 20 is [session_register("email");]

Answer: Sounds like you have your session_register() call too low in your code.\n\nThe script below checks a user name and password against a MySQL query and refers the user to a new page on successful login, or prompts them to try again on a failed login.\n\nNote that you don't need to explicitly register a session variable; instantiating it registers it.\n\n<?\nsession_start();\n\n$link = mysql_pconnect( 'localhost', 'username', 'password') or die( 'Cannot connect to db' );\nmysql_select_db( 'database' ) or die(' cannot select db' );\n\n$message = "Please log in."\n\nif( isset( $_POST['submit'] ) ) {\n$query = "SELECT * FROM login_id_table WHERE usercolumn = '" . mysql_escape_string( $_POST['user'] ) . "' AND passcolumn = '" . mysql_escape_string( $_POST['pass'] ) . "'";\n$rs = mysql_query( $query ) or die ('Cannot get login info' );\nif( mysql_num_rows( $rs ) > 0 ) {\n$_SESSION['loginid'] = $_POST['user'];\nheader( "Location: newpage.php" );\nelse {\n$message = "Login failed. Please try again."\n}\n}\n\n<html>\n<head>\n<title>Login Page</title>\n</head>\n<body>\n<h2>Login Form</h2>\n<p><? echo $message; ?></p>\n<form id="form1" name="form1" method="post">\n<p>User name: <input type="text" id="user" name="user" size="30" value="<% echo $_POST['user']; %>" /></p>\n<p>Password: <input type="text" id="pass" name="pass" size="30" value="<% echo $_POST['pass']; %>" /></p>\n<p><input type="submit" name="submit" id="submit" value="Submit" /></p>\n</form>\n</body>\n</html>
This question from the Yahoo Q&A forum is categorized in the topic: Sports