Forum Moderators: coopster

Message Too Old, No Replies

if.then and redirect

         

lovesirius12

2:14 am on Sep 12, 2006 (gmt 0)

10+ Year Member



hi, what do you think is wrong with this code?

<?php
$errmsg = "";
$complete = true;
if (isset($_POST['submit'])) {
$fname = ($_POST['firstname']);
$lname = ($_POST['lastname']);

if (empty($_POST[fname])) {
$errmsg = "Plese provide your FirstName";
$fname = "";
$complete = false;
}

if (empty($_POST[lname])) {
$errmsg = "Plese provide your LastName";
$fname = "";
$complete = false;
}

if ($complete == true) {
echo "<script>document.location.href='fine.html'</script>";
exit();
}
}
else
{
echo $errmsg;
}
?>

eelixduppy

2:26 am on Sep 12, 2006 (gmt 0)



Welcome to WebmasterWorld!

Try something like this:

[pre]
<?php
if (isset($_POST['submit']) &&!empty($_POST['fname']) &&!empty($_POST['lname'])) {
header('Location: fine.html');
exit();
} else {
echo 'You must fill out every field in the form!';
}
?>
[/pre]

Although it doesn't show a specific error message, I think it serves your purpose just as well ;)

Good luck!

P.S. What was wrong with your code was that the if conditional evaluated to TRUE, so the else never got called therefore not echoing out your error message.

rodan

6:47 pm on Sep 19, 2006 (gmt 0)

10+ Year Member



I am trying to do something similar by redirecting to another page on successful login, but my header function is not redirecting. Here is a portion of my code.

if (mysql_numrows($check)>0)
{
//redirect to member home page
header('Location:http://www.domain.org/page.htm');
exit;
}
else
{
echo "Invalid username and password found";
}

I am trying to redirect to a page that was built with FP. Does that make a difference?

eelixduppy

7:12 pm on Sep 19, 2006 (gmt 0)



Change this line:

if (mysql_numrows($check)>0)

To this:


if ([url=http://us3.php.net/manual/en/function.mysql-num-rows.php]mysql_num_rows[/url]($check)>0)

Watch those spelling errors ;)

Also, make sure that no content is being outputted to the browser before your header() call.

Good luck!

P.S. for future debugging, put error_reporting [us3.php.net](E_ALL); at the top of your script.

rodan

8:14 pm on Sep 19, 2006 (gmt 0)

10+ Year Member



I fixed the function call and put the error reporting in my code. I get the following error now, which I think is what you were referring to about the output to the browser.

Warning: Cannot modify header information - headers already sent by (output started at /homepages/34/d175731624/htdocs/login.php:8) in /homepages/34/d175731624/htdocs/login.php on line 101

Is there another function that I should be using to redirect my page to another page?

rokec

8:18 pm on Sep 19, 2006 (gmt 0)

10+ Year Member



To avoid errors like that (mysql_numrows), use program with coloured sintax, like dreamweaver.

And Rodan, you should remove all the html text, spaces and empty lines before header().

Publish your sourcecode and I'll find problem.

Hope helped you a bit.

rodan

8:33 pm on Sep 19, 2006 (gmt 0)

10+ Year Member



here is the code. I have a login form and then the test of the post

<!--#######################################login Information#################################################-->
<?PHP
if(!isset($_COOKIE["user"]))
{
echo '
<div id="loginInformation">
<form method="post">
<table cellpadding="0" cellspacing="0">
<tr>
<td>
Username:
</td>
<td>
<input type="text" name="loginUserID" style="width:100px; height:20px; vertical-align:middle; font-size:12px; ">
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<input type="password" name="loginPassword" style="width:100px; height:20px; vertical-align:middle; font-size:12px;">
</td>
</tr>
<tr>
<td>
<input type="submit" name="login" value="Login">
</td>
</tr>
</table>
</form>
</div>';
}
?>
<!--#######################################End Login Information#############################################-->
<?php
error_reporting(E_ALL);

if(isset($_POST['login']))
{
//connect to database
if ($link = mysql_connect("localhost", "#*$!", "#*$!"))
{
// select the database that will be used
mysql_select_db("#*$!") or die("An error has ocurred. Please contact the site administrator.");

//set post variables
$username = $_POST['loginUserID'];
$password = $_POST['loginPassword'];

// makes sure fields are completed
if(!$_POST['loginUserID'] ¦!$_POST['loginPassword'])
{
echo "Please enter the username and/or password";
die(" - Try again");
}

$check = mysql_query("SELECT * FROM members WHERE username = '$username' and passwd = '$password'");

//Gives error if user dosent exist
if (mysql_num_rows($check)>0)
{
header('Location:members_attorneys.htm');
exit;
}
else
{
echo "Invalid username and password found";
}

}
else
{
echo "No connection made to database";
}

}
?>

rokec

2:42 pm on Sep 20, 2006 (gmt 0)

10+ Year Member



Look. You have header('Location:members_attorneys.htm'); at the middle of the file. Headers must be at the beginning!

rodan

6:07 pm on Sep 20, 2006 (gmt 0)

10+ Year Member



What I am trying to do is go to another page on a successful login. I don't think "Header" is the function that I want to use if this is the case.

I'm still new to this PHP stuff, but things are beginning to make more sense.

If you have any suggestions on what function I should be using, thanks for any input.

rodan

8:05 pm on Sep 20, 2006 (gmt 0)

10+ Year Member



I just wanted to thank you for your patience, but I wanted to let you know that I figured it out.

I just added the form post to my current html page and the login code for test the username and password in the php file. It works great and now I just a little bit clearer understanding of how php executes and processes information.

Thanks

eelixduppy

8:16 pm on Sep 20, 2006 (gmt 0)



Make sure you escape your variables though:

$username = [url=http://us2.php.net/manual/en/function.mysql-real-escape-string.php]mysql_real_escape_string[/url]($_POST['loginUserID']);
$password = mysql_real_escape_string($_POST['loginPassword']);

Also make sure to validate that the user successully logged in when they visit other pages that require login.

Good luck!

rokec

5:03 pm on Sep 22, 2006 (gmt 0)

10+ Year Member



Instead of header() you can use javascript.
<script>
window.location="http://www.yourdomain.com/";
</script>
<noscript>
Javascript is diabled. Please enable it in your browser settings
</noscript>