Hello... I was wondering if anybody could help me find a solution to my rather annoying problem!! I used to run these scripts on another server without any hiccups... However, on the new server, I keep getting "Cannot Redeclare"; I have tried include_once though that didn't work either?

I've included the scripts here for you - it is a login system:

THE LOG-IN PAGE: (INDEX.PHP)

                      <form action="process_login.php" method="post" name="login" id="login">
                            <table width="56%" border="0" align="center" cellpadding="4" cellspacing="0">
                              <tr class="logtext">
                                <td width="42%"><span class="style35"> USERNAME</span></td>
                              <td width="58%">
                                  <input name="username" type="text" id="username2" size="15">
                                </td>
                              </tr>
                              <tr class="logtext">
                                <td><span class="style35">PASSWORD</span></td>
                              <td>
                                  <input name="password" type="password" id="password" size="15">
                                </td>
                              </tr>
                              <tr class="logtext">
                                <td>&nbsp; </td>
                                <td><input type="image" name="submit" src="images/enter.jpg" width="115" height="20"></td>
                              </tr>
                            </table>
                            <table width="100%" border="0" cellpadding="4" cellspacing="0">
                              <tr class="text">
                                <td width="100%" align="left" valign="top" class="style42 style41 style39 text"><div align="center"><strong><? echo $thanks_output; ?>
                                  <?php
if (isset($error_output))
{
    echo '<p class="logtext" text-align: center;">' . $error_output . '</p>';
}
?>
                                </strong></div></td>
                              </tr>
                            </table>
                            </form>

PROCESS_LOGIN.PHP (PROCESSES THE LOG-IN)

<?php
session_name("SID");
session_start();

include 'logging_functions.php';
include 'db.php';

if (isset($_POST['submit']))
{
	$username = $_POST['username'];
	$password = $_POST['password'];

if ((!$username) || (!$password))
{
	$error_output = 'Please enter your Username and Password!';
	include 'login.php';
	exit;
}

// Convert password to md5 hash
$password = md5($password);

// check if the user info validates the db
$sql         = mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$password' AND activated = '1'") or die(mysql_error());
$login_check = mysql_num_rows($sql);

if ($login_check > 0)
{
	while ($row = mysql_fetch_assoc($sql))
	{
		$_SESSION['username'] = stripslashes($row['username']);
		$_SESSION['first_name'] = stripslashes($row['first_name']);
		$_SESSION['last_name'] = stripslashes($row['last_name']);
	}
		// Cookie domain
		setcookie(session_name(), session_id(), (time() + 3600), '/', '', '0');

	header('location: main.php');
}
else
{
	$error_output = 'Incorrect username and/or password. Try again?';
	include 'index.php';
}
}
else
{
	// Form data wasn't available. Check for existing session.
	if (is_logged_in())
	{
		$error_output = 'You are already logged in.';
	}

include 'main.php';
}

?>

LOGGING_FUNCTIONS.PHP - LOGS PEOPLE IN/OUT:

<?php

function is_logged_in()
{
    if (isset($_SESSION['username']))
    {
        return true;
    }
    else
    {
        return false;
    }
}

function logout($destroy_cookie = true)
{
    if (is_logged_in())
    {
        $_SESSION = array();
        session_destroy();

    if ($destroy_cookie)
    {
        setcookie(session_name(), session_id(), (time() - 3600), '/', '', '0');
    }
}
}

?>

FINALLY, THE HEADER INSIDE THE MAIN.PHP PAGE:

<?php
session_name("SID");
session_start();

include 'logging_functions.php';

if (!is_logged_in())
{
    header('location: index.php');
}

?>

DB.PHP - only contains the database connection string u/name & p/wd.

Any help/advice you could offer would be greatly appreciated!

Have a great day...

Many Thanks,

MM2006 :rolleyes:

    I forgot to mention in the above post... The actual error:

    Fatal error: Cannot redeclare is_logged_in() (previously declared in /home/public_html/logging_functions.php:5) in /home/public_html/logging_functions.php on line 3

    I hope someone can help? Thanks... MM2006

      The problem is caused by your confusing use of include(). I recommend that you refactor the above to do the following:

      1. All require(s) are placed at the top of a script only. (Change include() to require() everywhere as it handles errors more sensibly)
      2. All included scripts should ONLY define functions, variables, constants and classes, not do anything else - especially outputting HTML.
      3. It's normally best to have a single master require() script whose job it is to require() everything else that will be required on every page. Then you can just require() that one, and you'll know that all the functions will be required exactly once.
      4. Never conditionally require() anything. That will just cause confusion.

      I suspect that the problem happens because you're conditionally including main.php right at the bottom of one of your other scripts - this then re-includes some stuff that's already been included.

      However, if you follow the tips above, it will all work and you'll have no problem.

      Mark

        might be calling this 'logging_functions.php' in process_login.php and db.php also. you can remove logging_functions.php twice if you are calling that in db.php. call directly the db.php

          Hi Mark - Thanks for your tips... I'm relatively new to PHP; therefore I'm not entirely sure how to implement your suggestions? 😕

          What I find perplexing is that it worked fine on another host, but not on the new host? I suppose it could be the PHP version? Not sure.

          So you think the error is caused by the conditional include of main.php? How do I get around that; since I do not wish for the content to be viewed by unauthorised viewers?

          Thanks for your help 🙂

          MM2006

          MarkR wrote:

          The problem is caused by your confusing use of include(). I recommend that you refactor the above to do the following:

          1. All require(s) are placed at the top of a script only. (Change include() to require() everywhere as it handles errors more sensibly)
          2. All included scripts should ONLY define functions, variables, constants and classes, not do anything else - especially outputting HTML.
          3. It's normally best to have a single master require() script whose job it is to require() everything else that will be required on every page. Then you can just require() that one, and you'll know that all the functions will be required exactly once.
          4. Never conditionally require() anything. That will just cause confusion.

          I suspect that the problem happens because you're conditionally including main.php right at the bottom of one of your other scripts - this then re-includes some stuff that's already been included.

          However, if you follow the tips above, it will all work and you'll have no problem.

          Mark

            Hi Thread_PHP,

            Thanks for your advice... db.php doesn't call logging_functions.php...

            DB.PHP:

            <? 
            /*  Database Information - Required!!  */
            /* -- Configure the Variables Below --*/
            $dbhost = 'localhost';
            $dbusername = 'usernamehere';
            $dbpasswd = 'passwordhere';
            $database_name = 'usersdb';
            
            /* Database Stuff, do not modify below this line */
            
            $connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd") 
            	or die ("Couldn't connect to server.");
            
            $db = mysql_select_db("$database_name", $connection)
            	or die("Couldn't select database.");
            ?>

            So I don't think tha's a problem? 😕

            Thanks,

            MM2006

            thread_PHP wrote:

            might be calling this 'logging_functions.php' in process_login.php and db.php also. you can remove logging_functions.php twice if you are calling that in db.php. call directly the db.php

              Hiya,

              Does anyone have any more suggestions please? Sorry for not being able to understand the implementation of Mark's tips 🙁

              Thanks...

              MM2006

                I managed to resolve this one - I changed the way the login was processed and the session was registered which in turn made the actual system easier.

                Thanks,

                MM2006

                  Write a Reply...