Forum Moderators: coopster

Message Too Old, No Replies

Can't get else if to work

elseif

         

jtsroberts

7:33 am on Apr 15, 2008 (gmt 0)

10+ Year Member



Hi,

I am very new to PHP, so please be nice :-)

I am trying to get this piece of code to work. What is happening is I have a page called index.php, that checks if a user is authenticated through a session variable. If they aren't they are sent to the page login.php. What I expect should happen is they are sent to the page with the code below and they are shown the 2nd form (the one at the bottom).

Can anyone see why this doesn't work? All I receive is a blank white screen? Source reveals just basic html tags.

<?php
session_start();
include "affiliate.config.php";
if (isset($_POST['submit']))
{
$query = "SELECT AffiliateUsername, AffilifatePassword, Active FROM tblaffiliates WHERE AffiliateUsername = '" . $_POST['username'] . "' AND AffilifatePassword = (password('" . $_POST['password'] . "')) AND Active = '1'";
$result = mysql_query($query) or die(mysql_error());

if (mysql_num_rows($result) == 1)
{
$_SESSION['logged'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
header ("Refresh: 2; URL=" . $_POST['redirect'] . "");
echo "You are being redirected to your original page request!<br>";
echo "(If your browser doesn't support this, <a href=\"" . $_POST['redirect']. "\">click here</a>)";
}
else
{
?>
<html>
<head>
<title>Log In</title>
</head>
<body>
Invalid Username and/or Password<br>
Not registered? <a href="../affiliate.php">Click here</a> to register.<br>
<form action="login.php" method="post">
<input type="hidden" name="redirect" value="<?php echo $_POST['redirect']; ?>">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br><br>
<input type="submit" name="submit" value="Log In">
</form>
</body>
</html>
<?
}
}
else
{
if ($_SERVER['HTTP_REFERER'] == "" ¦¦ $_SERVER['HTTP_REFERER'] == "http://localhost/appname/affiliates/index.php")
{
$redirect = "../affiliate.php";
}
else
{
$redirect = $_GET['redirect'];
}
?>
<html>
<head>
<title>Log In</title>
</head>
<body>
Login below by supplying your username/password...<br>
Or <a href="../affiliate.php">click here</a> to register.<br><br>
<form action="login.php" method="post">
<input type="hidden" name="redirect" value="<?php echo $redirect; ?>">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br><br>
<input type="submit" name="submit" value="Log In">
</form>
</body>
</html>
<?php
}
?>

Can anyone see why it wouldn't work? The MySQL connection shouldn't matter at this stage as the connection should not be occuring yet. I can't even get it to show the form!

deMorte

12:37 pm on Apr 15, 2008 (gmt 0)

10+ Year Member



Welcome to Webmaster World.

When I tested the code you supplied, I did get it to show the user input form (albeit I did leave out the include from the beginning). I think the problem is not with the if-else -structure.

Are you sure your code redirects from index.php to login.php? Could it be that the page showing no content is actually index.php?

Hope this helps.

[edited by: deMorte at 12:41 pm (utc) on April 15, 2008]

jtsroberts

12:57 pm on Apr 15, 2008 (gmt 0)

10+ Year Member



Thanks for the reply. No, the index.php file contains the following:

<?php
include "auth.inc.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
etc, etc, etc (just some more html)

***********
the auth.inc.php file contains:

<?php
session_start();
if ($_SESSION['logged'] != 1)
{
$redirect = $_SERVER['PHP_SELF'];
header("Refresh: 2; URL=login.php?redirect=$redirect");
echo "You are being redirected to the login page!<br>";
echo "(If your browser doesn't support this, <a
href=\"login.php?redirect=$redirect\">click here</a>)";
die();
}
?>

*************
the affiliate.config.php file contains:

// LOCAL CONNECTION INFORMATION
$cfg_local_db_host = '127.0.0.1';
$cfg_local_db_user = 'root';
$cfg_local_db_pass = 'blahblah';
$cfg_local_db_name = 'test_db';

$conn = mysql_connect($cfg_local_db_host, $cfg_local_db_user, $cfg_local_db_pass) or die(mysql_error());
$db = mysql_select_db($cfg_local_db_name) or die(mysql_error());

*************
Is there anything I am missing here?

jatar_k

1:43 pm on Apr 15, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld jtsroberts,

smells like you have a parse error that is not being shown

have you had errors show up in your browser? Maybe check your setting for display_errors [php.net]

jtsroberts

2:01 pm on Apr 15, 2008 (gmt 0)

10+ Year Member



Thanks for the reply. Yep, I have set display_errors to on in php.ini, however, I haven't restarted machine yet. In my experience only restarting IIS does nothing for PHP...not sure if it's even meant to. Anyways. I will restart and see if it makes a difference.

jtsroberts

4:32 am on Apr 16, 2008 (gmt 0)

10+ Year Member



Hi,

Well, after restarting, I now get nice error messages! So, I am actually getting two errors:

On the index.php page before the redirect fires, I get:
Notice: Undefined index: logged in C:\Inetpub\wwwroot\appname\affiliates\auth.inc.php on line 3

Then, on the login.php page, I get:
Parse error: syntax error, unexpected $end in C:\Inetpub\wwwroot\appname\affiliates\login.php on line 66
(this happens to be the very last line).

I am not sure about #1, but #2 I think is due to a missing } or something, not sure.

Any ideas?

[edited by: jtsroberts at 4:34 am (utc) on April 16, 2008]

coopster

12:48 pm on Apr 16, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The first one you need to look at line 3 of your auth.inc.php script to see where the 'logged' index is being used and why it is not defined yet.

The second message will be much easier to locate with a good text editor that highlights syntax errors. A search over the forums will turn up lots of options for programming text editors.