Jump to content

Redirection


Ken2k7

Recommended Posts

What's wrong with the redirection code here:

 

<link rel="stylesheet" type="text/css" href="css.css" />
<?php
session_start();

define("OPEN", 1);
require "config.php";
require "classes.php";

if ($_POST['submit'])
{
$name = $_POST['name'];
$pass = $_POST['pass'];
$error = "The username and password are incorrect.<br />If you are not registered, please <a href='register.php'>register</a>; otherwise, try again.";
$query = mysql_query("SELECT * FROM member WHERE name='$name'") or die(mysql_error());
if (mysql_num_rows($query) == 0) echo $error . "1";
else
{
	while ($row = mysql_fetch_assoc($query))
	{
		if (strtolower($row['name']) == strtolower($name))
			if ($row['password'] == sha1($pass))
			{
				$_SESSION['name'] = $row['name'];
				$loc = $_SESSION['name'];
				header("Location: $loc");
			}
			else echo $error . "2";
		else echo $error;
	}
}
}

if (isset($_SESSION['name'])) header("Location: {$_SESSION['name']}");
else
echo "<title>Coder's Alliance</title>
	<h1>Coder's Alliance Login</h1>
	<form action='$PHP_SELF' method='post'>
	Username: <input type='text' name='name' size='40' length='40'><br />
	Password: <input type='password' name='pass' size='40' length='40'><br />
	<input type='submit' name='submit' value='Submit'>
	<input type='reset' name='reset' value='Clear It'>
	</form>";
?>

Link to comment
Share on other sites

Not really sure what the problem is... try this:

if ($_SESSION['name']!="" && $_SESSION['name']!=NULL) header("Location: {$_SESSION['name']}");

instead of

if (isset($_SESSION['name'])) header("Location: {$_SESSION['name']}");

Maybe that helps?

Link to comment
Share on other sites

That's where the code stops and doesn't work. It doesn't record the session so the statement you wrote won't activate and it doesn't redirect either.

<?php
if (strtolower($row['name']) == strtolower($name))
if ($row['password'] == sha1($pass))
{
	$_SESSION['name'] = $row['name'];
	$loc = $_SESSION['name'];
	header("Location: $loc");
}

Link to comment
Share on other sites

Try this and report any errors:

 

<?php
error_reporting(E_ALL);
session_start();

function doLogin(){	
$resultset = mysql_query(
	'SELECT name, password FROM member WHERE name = "'.mysql_real_escape_string($_POST['name']).'"') 
or die(mysql_error());

if(($row = mysql_fetch_assoc($resultset)) !== false){
	if(strtolower($row['name']) == strtolower($_POST['name']) && $row['password'] == sha1($_POST['pass'])) 
	{
		$_SESSION['name'] = $row['name'];
		return true;
	}
}
return false;
}
function printForm(){
echo "<h1>Coder's Alliance Login</h1>
<form action='{$_SERVER['PHP_SELF']}' method='post'>
Username: <input type='text' name='name' size='40' length='40'><br />
Password: <input type='password' name='pass' size='40' length='40'><br />
<input type='submit' name='submit' value='Submit'>
<input type='reset' name='reset' value='Clear It'>
</form>";	
}
if(isset($_POST['submit'])) {
if(!doLogin()){
	printForm();
	echo "Login incorrect.<br />If you are not registered, 
	please <a href='register.php'>register</a>; 
	otherwise, try again.";
}
	else {
	header("Location: {$_SESSION['name']}");		
}
}
elseif(isset($_SESSION['name'])) {
	header("Location: {$_SESSION['name']}");
}
else {
printForm();
}
?>

Link to comment
Share on other sites

He means that you need

change
header("Location: $loc");
to
header("Location: ".$loc);

 

notice the . (period) in the second header line... for concatenation. You left that out, probably by mistake.

 

But really the two lines get interpreted the same by the PHP engine.

Link to comment
Share on other sites

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.