Jump to content

[SOLVED] Redirection?


Derleek

Recommended Posts

what is the syntax to redirect  a user to a specific page?

 

i have a script set up for user verification and i want it to redirect to a specific page when the user name and password are correct... can't seem to find the syntax for it in the php manual

 

Link to comment
Share on other sites

I'm validating the info on the login.php page so i don't know if header() will work...

 

does this require that i don't output any HTML data, or just don't use any PHP outputs?

 

how does meta refresh work?

Link to comment
Share on other sites

sorry, let me be more clear.

 

here is my code:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Login</title>
</head>
<body>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
screen name: <input type="text" name="login" maxlength="25"><br>
password: <input type="password" name="pword" maxlength="25"><br>
<input type="submit" name="submitbtn" value = "Login!">
</form>
or Signup <a href="SignUp.php">here</a> <br>
<?php
//validation code would be here...
     if(username && password are correct)
           {
                //would be code to send user to game.php
           }
}
?>
</body>
</html>

 

i'd prefer to do the validation with a script on login.php, as i understand it is more efficient. So maybe i am in need of a way to do this with out generating any output...

 

maybe the term i am looking for is not 'redirect'.  I simply need to send the user to a new page after the password and user name are varified

 

 

Link to comment
Share on other sites

Just move the validation to the top of the page.

 

<?php
//validation code would be here...
     if(username && password are correct)
           {
                //would be code to send user to game.php
           }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Login</title>
</head>
<body>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
screen name: <input type="text" name="login" maxlength="25"><br>
password: <input type="password" name="pword" maxlength="25"><br>
<input type="submit" name="submitbtn" value = "Login!">
</form>
or Signup <a href="SignUp.php">here</a>
</body>
</html>

Link to comment
Share on other sites

@blade: I assumed the validation included some kind of check to see if the form was submitted.

 

@op: Here's some basic code I always start with when I build a login page. Maybe you can get some use out of it too.

 

<?php
session_start();

function myEscape($string)
{
dbconnect();
$new = get_magic_quotes_gpc() ? stripslashes($string) : $string;
$safe = mysql_real_escape_string($new);
dbclose();
return $safe;
}

if ($_POST["Submit"] == "Login")
{
foreach ($_POST as $key => $val)
{
	$_POST[$key] = myEscape($val);
}
$un = $_POST['Username'];
$pw = md5($_POST['Password']);
dbconnect();
$sql = "SELECT * FROM table WHERE username='{$un}' AND password='{$pw}'";
$result = mysql_query($sql) OR DIE ("Unable to validate login.");
dbclose();
if (mysql_num_rows($result) > 0)
{
	$r = mysql_fetch_assoc($result);
	$user = $r["username"];
	$pass = $r["password"];
	if ($un == $user && $pw == $pass)
	{
		$_SESSION['Login'] = TRUE;
		header("Location: data.php");
		exit;
	}
	else
	{
		$_SESSION['Login'] = FALSE;
		$error = TRUE;
	}
}
else
{
	$_SESSION['Login'] = FALSE;
	$error = TRUE;
}
}

?>
<html>
<body>

<form action="login.php" method="post">
<p>
	<label for="Username" id="Username">Username:</label><br />
	<input type="text" name="Username" value="" maxlength="20" />
</p>
<p>
	<label for="Password" id="Password">Password:</label><br />
	<input type="password" name="Password" value="" maxlength="20" />
</p>
<p>
	<label for="Buttons" id="Buttons">Done?</label><br />
	<input type="submit" name="Submit" value="Login" /> - 
	<input type="reset" name="Reset" value="Reset" />
</p>
</form>

<?php
echo ($error) ? "<p class=\"error\">Login error.</p>" : "";
?>

</body>
</html>

Link to comment
Share on other sites

ok, i tried moving my script above the <html> tags and removing all of the output... but i still get the output error which is

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\motogame\Login.php:2) in C:\xampp\htdocs\motogame\Login.php on line 54

 

 

here is my entire login.php file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<?php
function sanityCheck($string, $type, $length){

  // assign the type
  $type = 'is_'.$type;

  if(!$type($string))
    {
    return FALSE;
    }
  // to check if there is anything in the string
  elseif(empty($string))
    {
    return FALSE;
    }
  // to check how long the string is
  elseif(strlen($string) > $length)
    {
    return FALSE;
    }
  else
    {
    // if all is well, we return TRUE
    return TRUE;
    }
}

function checkSet(){
  return isset($_POST['login'], $_POST['pword']);
  }
if(checkSet() !=FALSE)
{
if(empty($_POST['login'])==FALSE && sanityCheck($_POST['login'], 'string', 25)!=FALSE && empty($_POST['pword'])==FALSE && sanityCheck($_POST['pword'], 'string', 25)!=FALSE)
{
	$link = @mysql_connect('localhost', 'root', 'zOinks12');
	if (!$link) 
    {
		die('Not connected : ' . mysql_error());
    }	
	$db_selected = mysql_select_db('moto', $link);
	if (!$db_selected)
	    {
	    die ("Database not selected : " . mysql_error());
	    }
	$query = "SELECT realName, screenName, password, uID FROM fans";
	$result = mysql_query($query);
	while($row = mysql_fetch_array($result, MYSQL_ASSOC))
			{
				if($row[screenName]==$_POST['login'])
				{
					if($row[password]==$_POST['pword'])
					{
					header ("Location: moto_game.php");
					exit();
					}
					else 
					{
						exit();
					}
				}
			}
}
else
{
	exit();
}


}
?>

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Login</title>
</head>
<body>

<form action="Login.php" method="post">
screen name: <input type="text" name="login" maxlength="25"><br>
password: <input type="password" name="pword" maxlength="25"><br>
<input type="submit" name="submitbtn" value = "Login!">
</form>
or Signup <a href="SignUp.php">here</a> <br>
</body>
</html>

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.