Php redirect

Hi Guys,

im pretty noob at PHP

im wondering how to create a page that has a box where a user must enter a certain code, if the code matches a certain code they will be redirected to another page?

im guessing i will need to do something like asigning a variable with the correct code and then making an if statement that if the code entered matched the variable then redirect to link else give an error message

im just confused about how to put it in php :blush::blush:

duh i need a foreach function for arrays

i did this and it works

is this the okay?

if(‘POST’ === $_SERVER[‘REQUEST_METHOD’]){
foreach ($code as $value) {
if(false === empty($_POST[‘code’]) && $value === $_POST[‘code’]){
header(‘Location: http://www.google.com’);
exit;
}
}
$failed = true;
}

if(isset($_POST['submit'])){
if($_POST['txtValue']==$original_value){
header("location:yourpageurl");

}else{
echo "you have entered invalid code";
}

The $_POST[‘txtValue’] is the value entered by the user in the text box. And the $original_value is the variable you want to check.

You can also use captcha code here…

Don’t forget to place exit; after your header call, otherwise the script will continue executing. You should also use a full path for the location header too. :slight_smile:


<?php

error_reporting(-1);
ini_set('display_errors', true);

if('POST' === $_SERVER['REQUEST_METHOD'] && false === empty($_POST['txtValue'])){
  if($_POST['txtValue'] === 'S3cr3tP@s5wOrd'){
    header('Location: http://www.example.org/success.php');
    exit;
  }
}

?>

thanks guys, sorted now :slight_smile:

it’s blank because you don’t set anything to do when $_POST[‘submit’] is !isset


if(isset($_POST['submit'])){

}

add this up


else{
//do something here
}

and like what was already mentioned, exit after the header('Location…


header("Location: http://www.google.com");
exit();


<?php
error_reporting(-1);
ini_set('display_errors', true);

$code = '12345';

if('POST' === $_SERVER['REQUEST_METHOD']){
  if(false === empty($_POST['code']) && $code === $_POST['code']){
    header('Location: http://www.google.co.uk/');
    exit;
  }
  $failed = true;
}
?>
<html>
  <head>
    <title>Demo</title>
  </head>
  <body>
    <form action="" method="post">
      <input type="text" name="code" />
      <input type="submit" value="enter" />
    </form>
    <?php if(isset($failed)): ?>
      <p>
        Whoops, you've entered the wrong code
      </p>
    <?php endif; ?>
  </body>
</html>

hmmm i spoke to soon,

ive tried to style the page abit and now it comes out as a blank page?

here is my code


<?php 
$code = "code123";

if(isset($_POST['submit'])){
if($_POST['txt']==$code){
header("Location: http://www.google.com");
} else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
#wrap {
	width: 980px;
	margin: auto;
}

#content {
	width: 920px;
	border: 3px #cccccc solid;
	padding: 20px;
}

.error {
	color:#F30;
	font-weight:bold;
}
</style>
</head>
<body>
<div id="wrap">
<div id="content">
<p>Please enter the code to access the page</p>
<form method="post">
<fieldset>
<label for="txt">Enter Code: </label><input type="text" name="txt" id="txt" />
<input type="submit" name="submit" id="submit" value="submit" />
</fieldset>
</form>
<?php
echo "you have entered an invalid code";
}
}
?>
</div>
</div>
</body>
</html>

its soemthing to do with splitting the php code?

how do i use an array in this im trying


<?php
error_reporting(-1);
ini_set('display_errors', true);

$code = array();
$code[] = '12345';
$code[] = '6789';

if('POST' === $_SERVER['REQUEST_METHOD']){
  if(false === empty($_POST['code']) && $code === $_POST['code']){
    header('Location: http://www.google.com');
    exit;
  }
  $failed = true;
}
?>

<?php if(isset($failed)): ?>
        You have entered an invalid code
<?php endif; ?>

it doesnt seem to work?

You don’t need the foreach. :slight_smile:

if (false === empty($_POST['code']) && in_array($_POST['code'], $code)) {

thanks a lot guys i got it working :smiley: