homepage Welcome to WebmasterWorld Guest from 54.166.173.147
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Send visitor to page two
Send visitor to page two if password is keyed in.
Adam5000




msg:4282798
 4:01 am on Mar 17, 2011 (gmt 0)

Hi everyone.

I've got a little piece of code left to put in and it's about to make me lose my religion.

It seems simple really but I'm not having any luck with it.

What I'm trying to do is automatically send a visitor from page one to page two if the right words (in this case Corn flakes) are keyed into an html form.

It's like keying in a password to get from page one to page two.

Below is the code I've got. Page one is saved as page_one.php

Help!

<html>
<head>
<title>Page one</title>
</head>

<body>

<form action="page_one.php" method="post">
<input type="text" name="field_one" id="field_one">
<input type="submit" name="button" value="Enter">
</form>

<body>

<?php

if
($_post[field_one] != "Corn flakes")

echo "Wrong words";

else

//automatically send the visitor to page two. www.domain_name.com/page_two.htm

?>

</body>
</html>

 

brotherhood of LAN




msg:4282800
 4:10 am on Mar 17, 2011 (gmt 0)

//automatically send the visitor to page two. www.domain_name.com/page_two.htm

header('Location: www.domain_name.com/page_two.htm');
exit(0);

This will use a 301 re-direct to that page.

Adam5000




msg:4282906
 11:58 am on Mar 17, 2011 (gmt 0)

Thanks brotherhood and I'm still having a little trouble with it. When I insert the code to redirect the visitor I get an error message that reads "Cannot modify header information. Headers already sent."

I've removed all the white space (at least I think I have) and below is the bare bones code with the error in it.

The page is saved as test_page.php and the method is "post" so the form posts to itself.

At this point that's all I know.

Help!

<html>

<head>
<title>Test page</title>
</head>

<body>

<form action="test_page.php" method="post">
<input type="text" name="First_name">
<input type="submit" value="Enter">

<?php
if ($first_name=="Jane")
echo "Jane is here";
else
header('Location: www.google.com');
exit(0);
?>

</form>

</body>

</html>

brotherhood of LAN




msg:4282938
 1:31 pm on Mar 17, 2011 (gmt 0)

You won't want PHP to send any output before the HTTP headers, as per the description in the manual [php.net]

Using the logic you've provided, you may be better off putting it at the top of the page.

<?php
if($first_name != 'Jane')
{
header('Location: http://www.google.com/');
exit(0);
}
?>
<html>
....

Because you're going to redirect the user, the rest of the page is pretty much obsolete in their case, hence you can put it at the top of the script and let the rest of your code deal with the non-redirected users.

Adam5000




msg:4282957
 2:26 pm on Mar 17, 2011 (gmt 0)

Thanks again brotherhood and I'm still having trouble with it. I don't know where the original header information can be coming from. I kept removing lines of code until the only thing left was the redirection code and the error is still there. If I'm thinking right, the code I have below should be an automatic redirect in all cases. I'm mystified. Below is the entire complete test page (all eleven lines of it) and I still get the error. I'm mystified.

<html>
<head>
<title>Test</title>
</head>
<body>
<?php
header('Location: www.google.com');
exit(0);
?>
</body>
</html>

brotherhood of LAN




msg:4282962
 2:30 pm on Mar 17, 2011 (gmt 0)

I'd suggest having the script as:

<?php die('|');?>

This will display a pipe character and then stop. Look at the source code and you'll see if there are any characters before it that would cause issues when sending headers within this particular script.

Otherwise this should work fine:

<?php
header('Location: www.google.com');
exit(0);
?>
<html>
<head>
<title>Test</title>
</head>
<body>
</body>
</html>

rocknbil




msg:4283092
 4:30 pm on Mar 17, 2011 (gmt 0)

Any HTML - even a space or newline - that **precedes** your PHP will issue a content-type header. You'll also need some logic to control it. The below is a bit cumbersome but will do it. I also noted it might be the case of $_POST versus $_post, see notes in comments . . . though this is the first time I've ever played with it lower case. :-)


<?php
$currval=$error=null;
// Make post CAPS, it won't work on some servers/versions otherwise
// Didn't on the server I tested it on, anyway . . .
if (isset($_POST['field_one'])) {
if ($_POST['field_one'] == "Corn flakes") {
header("location:https://www.google.com");
exit; // not really necessary, just retentive that way . . .
}
else {
$currval=$_POST['field_one']; // NOT CLEANSED, for example only!
$error="&quot;$currval&quot; is the wrong word.";
}
}
?>
<html>
<head>
<title>Page one</title>
</head>
<body>
<?php if ($error) { echo "<p>$error</p>"; } ?>
<form action="formtest.php" method="post">
<input type="text" name="field_one" id="field_one" value="<?php echo $currval ?>">
<input type="submit" name="button" value="Enter">
</form>
</body>
</html>


This would display the form at first. Since $currval and $error are set to null, they won't display anything (and squelches "undefined variable" errors on the first load.) When submitted it checks the value, if it's wrong, populates those values and displays them in the form.

You can also change this line to make it case-insensitive and allow for sloppy spacing. :-)

if (preg_match('/^\s*corn\s+flakes\s*$/i',$_post['field_one'])) {

Adam5000




msg:4283098
 4:40 pm on Mar 17, 2011 (gmt 0)

I think the original header information is coming from the opening html tag. When I put the PHP code above the opening html tag the error message disappears.

However when I add in the redirection code it makes the page go blank.

This code works

<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<?php
if ($response_array[1]=="Red")
echo "This works";
?>
<html>
html code here
</html>

And this code makes the page go blank.

<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<?php
if ($response_array[1]=="Red")
header('Location: [google.com...]
exit(0);
?>
<html>
html code here
</html>

Adam5000




msg:4283373
 11:32 pm on Mar 17, 2011 (gmt 0)

Thanks guys. I stopped today about noon and I'm going to look at it again tomorrow after a night's sleep. I can't be more than a hair off.

rocknbil




msg:4283747
 4:43 pm on Mar 18, 2011 (gmt 0)

That's what I'm saying - since your HTML *preceded* the PHP in your original, it outputs a content-type header, so when you try to output the location header it errors. Probably goes blank because PHP error reporting is off.

In your latest example, the first code works because you're not printing the location header. As soon as you add the location header **after** any text is output, it will error. Try that scriptlet I posted, it was tested.

Also understand what the parameter does in exit(0) [php.net]. There's (usually) no need to return an exit status.

If you structure your logic correctly, there's no need to exit after a location header anyway . . .

if (! $doing_something) {
header("location:https://www.google.com");
}
else {
doing_something();
}
// There should be no code here to continue after your header . . .

Adam5000




msg:4283861
 8:38 pm on Mar 18, 2011 (gmt 0)

That code works rocknbil and I知 having trouble adapting to my form. I知 just going to try to explain what I知 trying to do.

I知 setting up a payment gateway for the members area of my website. A person who wants to join the site keys in a credit card number, an expiration date, and the code on the back of the card and clicks the submit button.

The information is then sent to the site that processes the transaction and there the transaction will either be approved or not.

Either way a response message is sent back to my site.

In all cases the response message is found in the variable $response array[3]

Possibilities include 的nvalid card number or 的nvalid expiration date or 典his transaction has been approved. And there could be other messages too. It depends on the specific situation.

If the transaction is approved then I want to send the person to the username and password selection page where they select a username and password and go from there.

If the transaction is not approved they would correct the error and resend.

Below is the stripped down code I've got and I don't understand why it doesn't work.

<?php
if (isset($response_array[3]))
{
if ($response_array[3]=="(TESTMODE) This transaction has been approved.")
{
header("location:https://username and password creation page");
exit;
}
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form action="060_signup_form.php" method="post">
<p>Credit card number</p>
<input type="text" name="card_number">

<?php
$post_url = "https://prossing page at credit card processor site.dll";
$post_values = array

(
"x_login"=> "login number",
"x_tran_key"=> "transaction key",

"x_version"=> "3.1",
"x_delim_data"=> "TRUE",
"x_delim_char"=> "|",
"x_relay_response"=> "FALSE",

"x_type"=> "AUTH_CAPTURE",
"x_method"=> "CC",

"x_card_code"=> "$_POST[c_code]"
);

$post_string = "";
foreach( $post_values as $key => $value )
{
$post_string .= "$key=" . urlencode( $value ) . "&";
}
$post_string = rtrim( $post_string, "& " );
$request = curl_init($post_url);

curl_setopt($request, CURLOPT_HEADER, 0);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE);
$post_response = curl_exec($request);

curl_close ($request);

$response_array = explode($post_values["x_delim_char"],$post_response);

echo
$response_array[3];
?>

</form>
</body>
</html>

Adam5000




msg:4283979
 1:31 am on Mar 19, 2011 (gmt 0)

Or in other words, I can send the information entered into the form to the credit card processor and get the "This transaction has been approved." message back. That part is working and done.

Now all I have to do is send the people (and only the people) who have been approved to the "Choose a username and password" page.

The "Choose a username and password page" is working and done too and it will take them through the rest of the signup process.

But at this point I don't have a way to send people who have paid from the page with the form on it to the choose a username and password page.

rocknbil




msg:4285161
 5:10 pm on Mar 21, 2011 (gmt 0)

I've never been a fan of redirecting. :-) Second, I'm seeing a potential problem in keeping "the person that's just paid" connected with the create account page - seems like someone could hack into that quite easily. Why can't you structure it something like this? I'm removing a lot of code to expose the logic.

if (! $some_form_input or ($some_from_input_and_error)) {
output_form_here($title,$error);
}
else {
// post your curl . . .
$response_array = explode($post_values["x_delim_char"],$post_response);
if ($the_response_is_good) {
output_the_login_form_here();
}
else {
output_form_here('credit card error',$error);
}

That's how I'd begin to approach it, no one can get to the account creation form without posting a successful payment.

Adam5000




msg:4286457
 8:16 pm on Mar 23, 2011 (gmt 0)

Thanks rocknbil That looks like a good plan and a better one than the one I'm working with. I'll try working with that.

In the meantime, I don't have a solution but about 30 minutes ago I think I've found the problem with the code I'm trying now. I think I'm trying to use an if statement with a variable before the variable has been assigned a value. For example this displays the words "this works" on the screen

<?php
$test="5";

if ($test=="5")
{
echo "this works";
}
?>

And this gives a blank screen.

<?php
if ($test=="5")
{
echo "this works";
}
$test="5";
?>

The variable $response_array[3] doesn't have a value before the form information is processed by the php code in the body. There are no errors in the code but there's no redirect because at first the variable $response_array[3] doesn't have a value.

You're terrific rocknbil. I'm glad you're here to help. I'll let you know how it goes.

Adam5000




msg:4286612
 12:50 am on Mar 24, 2011 (gmt 0)

That's a good idea rocknbil and for now I'm going to go with the redirect idea and use your better method later when I can research the additional code needed to make it work.

Do you have any ideas on a solution to the problem with the redirection code. The problem is I'm using an if statement with a variable that hasn't been given a value yet. See above post.

Adam5000




msg:4287013
 7:26 pm on Mar 24, 2011 (gmt 0)

Success

Take the php code (that's in the body of the html code) that processes the transaction and put it at the top of the page BEFORE the if statement with the redirect code in it. ALL php code before the opening <html> tag.

Whew.

Everyone, that was tough for my little bitty brain. I mean like Jeeze. I'm NOT rocknbil. Smile

rocknbil




msg:4287453
 3:49 pm on Mar 25, 2011 (gmt 0)

Me neither, I'm his evil doppelganger.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved