Jump to content

Recommended Posts

This is my login script for a blogsite...

 

<?php
require("config.php");
if($_POST['submit']) {
if (!$db) {
	$db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Could not connect to the database... Please try again later!");
	mysql_select_db($dbdatabase, $db);
}
$sql = "SELECT * FROM logins WHERE username = '" . $_POST['username'] . "' AND password = '" . $_POST['password'] . "';";
$result = mysql_query($sql) or die("Query failed: " . mysql_error());
$numrows = mysql_num_rows($result);
if($numrows == 1) {
	$row = mysql_fetch_assoc($result);
	session_start();
	$_SESSION['USERNAME'] =  $_POST['username'];
	$_SESSION['USERID'] = $row['id'];
	header("Location: " . $config_basedir . "index.php");
} else {
	$username = $_POST['USERNAME'];
	header("Location: " . $config_basedir . "/login.php?error=1");
}
}
else {
require("header.php");
}
if($_GET['error']) {
echo "Incorrect login, please try again!";
}
?>
<h2>Login</h2>
<div class="meta"><?php echo date("F j, Y @ g.iA",time()); ?></div>
<br>
<div>
<form action="<?php echo $SCRIPT_NAME ?>" method="post">
<table>
	<tr>
		<td>Username</td>
		<td><input type="text" name="username" value="<?php echo $username ?>"></td>
	</tr>
	<tr>
		<td>Password</td>
		<td><input type="password" name="password" value=""></td>
	</tr>
	<tr>
		<td></td>
		<td><input type="submit" name="submit" value="Login"></td>
	</tr>
</table>
</form>
</div>

<?php
require("footer.php");
if ($db)
mysql_close($db);
?>

 

 

But when executed it gives the error concerning page redirection:

 

Untitled.jpg

 

I dont understand where is the problem on the code.... can someone help me please... :-[

Link to comment
https://forums.phpfreaks.com/topic/156124-page-redirection-problem-in-login-page/
Share on other sites

Hey i have seen the post on Header errors and have already checked my script for any kind of output prior to redirection... my script does not output anything if it needs to redirect, only a session gets started... but i think that does not cause the problem....

 

Here is the config.php:

<!--
Defines configuration parameters
-->
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "";
$dbdatabase = "blogtastic";
$config_blogname = "Blogtastic";
$config_author = "Pooja Gupta & Mukul Sharma";
$config_basedir = "http://127.0.0.1/bb/New%20Folder/";
?>

 

In your code this part :

 

<!--
Defines configuration parameters
-->

Is a html comment and will be output and see by everyone when they view the source. That what broke the header() function used by session_start(). Use PHP comment like that :

 

<?php
/* -- Defines configuration parameters -- */
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "";
$dbdatabase = "blogtastic";
$config_blogname = "Blogtastic";
$config_author = "Pooja Gupta & Mukul Sharma";
$config_basedir = "http://127.0.0.1/bb/New%20Folder/";
?>

 

You won't get any output visible by the client and fix the problem.

avvllvva solution will work for session_start() but not for the header(location:...) later in the php file.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.