Hi all,

I am creating a online c.v
but do not want it open to every one so i have made a simple login file
but want the page to re-direct when the login is correct,

<?php
$user = "user";
$pass = "pass"

if ($_POST["username"] != $user && $_POST["pass"] != $pass ) { ?>

<form method="post" action="index.php" >
Username: <input type="text" name="username" />
<br/>
Password: <input type="text" name="pass" />
<br/>
<input type="submit" value="login" />

<?php } else { ?>

Now how would i re-direct the person i know how to re-direct them when the page loads but not this way.
Im confused with thisand would like to get my c.v up online asap so any help would be very appriciated
Thank you

Recommended Answers

All 8 Replies

Perhaps the following code would be of some use to you.

<?php
//no html/browser output before this function
header('Location: http://www.example.com/');
exit;
//...

Hi all,

I am creating a online c.v
but do not want it open to every one so i have made a simple login file
but want the page to re-direct when the login is correct,

<?php
$user = "user";
$pass = "pass";

if ($_POST["username"] != $user && $_POST["pass"] != $pass ) { ?>

<form method="post" action="index.php" >
Username: <input type="text" name="username" />
<br/>
Password: <input type="text" name="pass" />
<br/>
<input type="submit" value="login" />

<?php } else { ?>

Now how would i re-direct the person i know how to re-direct them when the page loads but not this way.
Im confused with thisand would like to get my c.v up online asap so any help would be very appriciated
Thank you

<?php
//connect ur dabase.
$user = "username";
$pass = "pass"

if ($_POST["username"] != $user && $_POST["pass"] != $pass ) { ?>

<form method="post" action="index.php" >
Username: <input type="text" name="username" />
<br/>
Password: <input type="text" name="pass" />
<br/>
<input type="submit" value="login" />

<?php } else { 
$select=mysql_query("select * from tablename where username=$user and password=$pass");
$count=mysql_num_rows($select);
if($count==1)
{
//redirect page

header('Location: http://www.example.com/');

exit;
}else
{
echo "login failed";
}
}
?>
<?php
//connect ur dabase.
$user = "username";
$pass = "pass"

if ($_POST["username"] != $user && $_POST["pass"] != $pass ) { ?>

<form method="post" action="index.php" >
Username: <input type="text" name="username" />
<br/>
Password: <input type="text" name="pass" />
<br/>
<input type="submit" value="login" />

<?php } else { 
$select=mysql_query("select * from tablename where username=$user and password=$pass");
$count=mysql_num_rows($select);
if($count==1)
{
//redirect page

header('Location: http://www.example.com/');

exit;
}else
{
echo "login failed";
}
}
?>

Just keep in mind there is a T_if error there and a column ( ; ) will need adding on line 4.

forgot to mention there is no mysql database in use, every one use's the same login info

<?php
//since no database is being used, below is the username/password for everybody
$username = "username";
$password = "password";

if (($_POST['username'] == $username) && ($_POST['password'] == $password) ) { ?>

header('Location: nextPage.php');
exit;

} else { ?>

<form method="post" action="index.php" >
Username: <input type="text" name="username" />
<br/>
Password: <input type="text" name="password" />
<br/>
<input type="submit" value="login" />

<?php

}

?>

header(); works fine, though not always.. i found this little treasure on the web.. it's better to use..

$url = "whereto.php"; // target of the redirect
$delay = "0"; // seconds of delay
echo '<meta http-equiv="refresh" content="'.$delay.';url='.$url.'">';
die();

just put this in place of


header('Location: nextPage.php');
exit;

header(); works fine, though not always.. i found this little treasure on the web.. it's better to use..

$url = "whereto.php"; // target of the redirect
$delay = "0"; // seconds of delay
echo '<meta http-equiv="refresh" content="'.$delay.';url='.$url.'">';
die();

just put this in place of


header('Location: nextPage.php');
exit;

Also if you want to delay the header you can use the following.

$delay=2; //seconds
sleep($delay);
header('Location: nextPage.php');
//no html output before the header function
exit;

And in addition the header function is always much faster providing you use the exit; line. This is because the user doesn't need to downloading any content to be redirected where as the html method an entire page would need to be transfered. Also note that the meta redirect is not compatible with all browsers unlike the header function.

@cwarn23 is also correct.. i guess you can just try which solution best suits your work.. here is a link to some additional info regarding headers() http://www.jonasjohn.de/snippets/php/headers.htm

@cwarn23 - good work.. thanks for the added info..

Also if you want to delay the header you can use the following.

$delay=2; //seconds
sleep($delay);
header('Location: nextPage.php');
//no html output before the header function
exit;

And in addition the header function is always much faster providing you use the exit; line. This is because the user doesn't need to downloading any content to be redirected where as the html method an entire page would need to be transfered. Also note that the meta redirect is not compatible with all browsers unlike the header function.

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.