Page 1 of 2

Form not redirecting

Posted: Mon Nov 22, 2004 5:07 am
by TheOracle
I have written a simple login script, which checks the users input and redirects based on a variable parsed by the URL.

The forms action= is set to $_SERVER[PHP_SELF], and when incorrect information is input, the error information is displayed, so the code is working, but if you put in the correct information it just refreshes the page!

I have posted the code at the top of the web page below. I am using PHP5.

Code: Select all

<?php 
include("includes/dbconn.inc"); 
include("includes/funclib.inc"); 

$message_header ="<span class='style16'><b><BR><BR>We were unable to complete your login due to the following error(s):<b><BR></span>"; 

if (array_key_exists('actionflag',$_POST)) 
	{ 
    $actionflag=$_POST['actionflag']; 
    $login=$_POST['login']; 
    $password=$_POST['password']; 
    $password2=$_POST['password2']; 
	} 

if(isset($actionflag)) 
    { 
    if(!$login || !$password || !$password2) 
        $message = "<font size=1 color='#CC0000'>You must fill in all fields before applying<BR></font> \n"; 
    if($password!=$password2) 
        $message .= "<font size=1 color='#CC0000'>Your passwords did not match<BR></font> \n"; 
    if(strlen($password) >  
        $message .= "<font size=1 color='#CC0000'>Your password must be less than 8 characters<BR></font> \n"; 
    if (!preg_match('/^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/',$login)) 
        $message .= "<font size=1 color='#CC0000'>Please enter a valid email address</font>" ; 
    if(getRow("user_details","username", $login)) 
        $message .= "<font size=1 color='#CC0000'>The email address " .$login. " has already been used. Please contact us if you wish to make another application<BR></font> \n"; 
    if(!$message) 
    { 
    $id = newUser($login, $password); 
    cleanMemberSession($id, $login, $password); 
    if ($_GET["button"] == 1) 
    { 
       header("Location:page1.php".SID); 
       exit(); 
    } 
    if ($_GET["button"] == 2) 
    { 
       header("Location:page2.php".SID); 
       exit(); 
    } 
    if ($_GET["button"] == 3) 
    { 
       header("Location:page3.php".SID); 
       exit(); 
    } 
    if ($_GET["button"] == 4) 
    { 
       header("Location:page4.php".SID); 
       exit(); 
    } 
    if ($_GET["button"] == 5) 
    { 
       header("Location:page5.php".SID); 
       exit(); 
    } 
    if ($_GET["button"] == 6) 
    { 
       header("Location:page6.php".SID); 
       exit(); 
    } 
} 
?>
The variable "button" is being parsed by the sending URL ie. auth.php?button=1

Hope someone can help.

Posted: Mon Nov 22, 2004 6:24 am
by dull1554
if(!$login || !$password || !$password2) '

try using
if((!isset($login)) || (!isset($password)) || (!isset($password2)))

Posted: Mon Nov 22, 2004 7:35 am
by TheOracle
Sorry, no joy.

It is not the validation that doesn't work. That part is fine.

It is if I put in the correct details it does not forward me onto the relevant page, merely refreshes.

Any other ideas?

Posted: Mon Nov 22, 2004 8:15 am
by John Cartwright
change $_GET['button'] to $_POST['button']

Posted: Mon Nov 22, 2004 8:55 am
by TheOracle
Thanks Phenom, but still no joy.

It is inserting the data into the database, so its reaching the NewUser and cleanMemberSession functions, but does not appear to be doing anything with the headers? i.e. no redirection - still refreshing the page!

Any other thoughts?

Posted: Mon Nov 22, 2004 10:31 am
by phpScott
first I think it would be easier for your if statements to be in a switch statement but that may just be me.

Code: Select all

<?php

switch($_POST['button'])
case '1':
  header("Location:page1.php".SID); 
	break;
case '2':
  header("Location:page2.php".SID); 
	break;
case '3':
  header("Location:page3.php".SID); 
	break;
case '4':
  header("Location:page4.php".SID); 
	break;
case '5':
  header("Location:page5.php".SID); 
	break;
case '6':
  header("Location:page6.php".SID);
	break;
default:
  header("Location:somedefaultPage.php".SID); 
?>
then you could also set up a default page to catch anything else that doesn't fit or fails.
Secondely I am assumint(yes I've heard the jokes) that SID is = to something like ?someName=someValue

thirdly echo out what $_POST['button'] is as it may be something your not expecting.

and finally try putting single quotes around your values in your if satement if you decide not to use the switch statement as button may be coming in as a string and not a int and although php is typed somethimes you can get some weird things happeining
:?
phpScott

Posted: Mon Nov 22, 2004 10:43 am
by TheOracle
OK, I'll try this and let you know how I get on.

Many thanks

Posted: Mon Nov 22, 2004 11:13 am
by Maugrim_The_Reaper

Code: Select all

<?php
header("Location:page1.php".SID); 
?>
Maybe it's the header having some fun? Try a full uri address to page#.php.

Code: Select all

<?php
header("Location: http://foo.bar/page1.php" . SID);
?>
I've given up on using header() for redirection because it's had some hideous problems in the past on various browsers (well, one at least :)) - there's a javascript alternative if required.

Code: Select all

<?php
echo "<script>self.location='page1.php" . SID . "';</script>";
?>

Posted: Mon Nov 22, 2004 5:13 pm
by TheOracle
Sorry phpScott, but your switch caused an error, and I couldn't get it work with single quotes either.

I don't really want to incorporate java. Just want to get this to redirect!

Posted: Mon Nov 22, 2004 5:18 pm
by Archy
If you havent already, try adding ob_start(); to the top of your page, im not sure if you have it in one of your includes though.

Posted: Mon Nov 22, 2004 6:11 pm
by evilmonkey
Why not use simple html <meta> tags to redirect? I've always found the header() function to have odd behaviour.

Posted: Mon Nov 22, 2004 11:50 pm
by TheOracle
No joy with ob_start(), any other suggestions? How would the meta tag redirection work?

Posted: Tue Nov 23, 2004 1:26 am
by Archy

Code: Select all

<META HTTP-EQUIV=Refresh CONTENT="10; URL=http://www.domain-name.com/">
This would refresh to http://www.domain-name.com/ after 10 seconds, you can change the CONTENT=10; to anything to make the page redirection faster or slower.

Posted: Tue Nov 23, 2004 2:14 am
by TheOracle
But can I add that in my if statements or does it have to be at the top in the html head tag?

Posted: Tue Nov 23, 2004 3:06 am
by phpScott
what is SID suppossed to be? as that may be causing the problem.
Right now it is defined as a constant but what is it really?