Form not redirecting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

Form not redirecting

Post 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.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

if(!$login || !$password || !$password2) '

try using
if((!isset($login)) || (!isset($password)) || (!isset($password2)))
TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

change $_GET['button'] to $_POST['button']
TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

Post 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?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post 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
TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

Post by TheOracle »

OK, I'll try this and let you know how I get on.

Many thanks
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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>";
?>
TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

Post 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!
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post 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.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Why not use simple html <meta> tags to redirect? I've always found the header() function to have odd behaviour.
TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

Post by TheOracle »

No joy with ob_start(), any other suggestions? How would the meta tag redirection work?
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post 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.
TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

Post 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?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post 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?
Post Reply