Page 1 of 1

Getting value from input form

Posted: Sat Aug 07, 2010 8:22 pm
by marvolo1300
Hi, i created a login script. it should ask the user to submit an access id then if it is correct, it will redirect the user to another folder.

I think i did it correctly but it gives me this error:

[text]The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

* This problem can sometimes be caused by disabling or refusing to accept
cookies.[/text]

This is my script :

Code: Select all

<title>Marv's Website</title>

<?php

echo "Please enter your Access ID ";

?>


<form name="form1" method="post" action="">
  <input type="password" name="ui_login" id="ui_login">
</form>
<a href="index.php~">index</a><input name="Submit" type="button" value="Submit">

<?php
$realid="google";
$access_id=$_POST['user'];

if ('$access_id'=='$realid')
{
header('Location: http://192.168.1.105/Local Sharing');
echo "Access ID approved";
}

else
{
	header ('Location: http://192.168.1.105/index.php');
	echo "Login failed";
}

?>

Re: Getting value from input form

Posted: Sat Aug 07, 2010 9:33 pm
by requinix
There's something you haven't mentioned that we need to know. Not sure what, though.

1. Problem:

Code: Select all

if ('$access_id'=='$realid')
Variables don't work in single-quoted strings, so that there is actually comparing "$access_id" with "$realid". Which will never match. Which means you're always be redirected to /index.php. Somehow, index.php is redirecting to whatever page that is automatically. Which will redirect back to the index. Which is the redirect cycle that never ends - it just goes on and on, my friends. Some script started redirecting, not knowing where it went, and it'll continue redirecting forever just because it is the redirect cycle that never ends... Woah. Where'd that come from?

If you're comparing variables, don't use quotes. They just plain aren't necessary.

Code: Select all

if ($access_id==$realid)
2. There's another issue: you need to check if the form was submitted. If it wasn't, $_POST['user'] will be null and

Code: Select all

null == "google"
will never be true and you'll always be redirected. Without even getting the chance to submit the form in the first place.

Code: Select all

$realid="google";
if (!empty($_POST['user'])) {
        $access_id=$_POST['user'];

        if ($access_id==$realid)
        {
                header('Location: http://192.168.1.105/Local Sharing');
                echo "Access ID approved";
        }

        else
        {
                header ('Location: http://192.168.1.105/index.php');
                echo "Login failed";
        }
}
3. One more thing. You can't redirect with header() if you've outputted something. You did: the HTML page and form.
Put that redirection stuff at the top of the file.


Here's the kicker: unless you're using output buffering, none of that has anything to do with the redirection problem. Which is why I'm not sure what else you need to tell us.

Re: Getting value from input form

Posted: Sat Aug 07, 2010 10:01 pm
by marvolo1300
A few questions:

1. What is output buffering ?
2. How can i redirect with out using header?

Sorry, i didn't explain fully, i want to redirect the user to a directory for downloading files. Also, i added comments on the code

Code: Select all

<?php

echo "Please enter your Access ID ";

?>

/* A form for asking the user for the access id */

<form name="form1" method="post" action="">
  <input type="password" name="ui_login" id="ui_login">
</form>
<a href="index.php~">index</a><input name="Submit" type="button" value="Submit">


/* Declaring variables */
<?php
$realid="google"; /* The string where the real access id is stored */
$access_id = $_POST ['ui_login']; /* Assigns the value of what the user inputted to $access_id */


if ($access_id==$realid) /*compares what the user inputted with the realid */
{
header("Refresh: 3; http://192.168.1.105/Sharing Files/"); /* if the what the user
													inputted is the same as the real id then it 				                                                     will redirect the user to where he/she wants 														to go*/

echo "Access ID approved"; /* if the what the user													inputted is the same as the real id then it will display "Access ID approved" */
}

else /*If $access_id is not equal to $realid  */
{
header("Refresh: 3; http://192.168.1.105/index.php/") /*It will redirect/refresh the page*/
echo "Login failed"; /*Displays "Login failed" */
}

?>

Re: Getting value from input form

Posted: Sat Aug 07, 2010 10:24 pm
by requinix
1. Output buffering is when everything you output (with an echo/print, or stuff outside the <?php tags, etc) gets held internally and isn't actually outputted to the client until you say so. Don't worry about it for now.
2. Use header() to redirect. That's correct. But it doesn't work unless there has been no actual output up to that point. Restructure your script so that is the case.

Code: Select all

header("Refresh: 3; http://192.168.1.105/Sharing Files/");
That behavior is not in the HTTP specification. Do not use it.
With header() you redirect immediately - the user shouldn't see anything on the page. If you want a pause before redirecting you need to use a <meta> tag (where the timeout;url= format is allowed) or JavaScript.

Back to the infinite redirection problem: as a web developer you should have Firefox installed with the Firebug extension minimum. Use that extension to watch what URLs Firefox is trying to redirect to. With those in hand it should be easier to figure out what is sending you where - and why.

Re: Getting value from input form

Posted: Sun Aug 08, 2010 2:14 pm
by marvolo1300
If you want a pause before redirecting you need to use a <meta> tag (where the timeout;url= format is allowed) or JavaScript.
I can't use any tags in an if statement. what should i use?

Re: Getting value from input form

Posted: Sun Aug 08, 2010 11:22 pm
by requinix
marvolo1300 wrote:I can't use any tags in an if statement.
That doesn't make sense. What can't you do?