erol_smsr
erol_smsr

Reputation: 1496

Constant already defined PHP

I have a class file called User.class.php and inside this class I have this piece of code:

<?php
    define('__ROOT__', dirname(dirname(__FILE__))); 
    require_once(__ROOT__.'/mysite/generic.php');
?>

I have another file, called signup.php, and I need to use three classes, so I have code that looks like this:

<?php
    define('__ROOT__', dirname(dirname(__FILE__))); 
    require_once(__ROOT__.'/mysite/classes/dbhandler.class.php');
    require_once(__ROOT__.'/mysite/classes/User.class.php');
    require_once(__ROOT__.'/mysite/includes/password_hash.php');
?>

Every time I run signup.php I get this error:

Constant __ROOT__ already defined in signup.php

I've tried something similar to, but this also produces the same error:

<?php
    if(!defined('__ROOT__')) {
        define('__ROOT__', dirname(dirname(__FILE__)));
    }
?>

The signup.php produces an error, but does execute an SQL insert query. I have another file called sign_2.php, which has to execute an SQL UPDATE query, which also produces the same error when I execute it, but it doesn't execute the UPDATE query. It runs the code (no PDOException thrown), but the query doesn't affect a single row (and the query does work in PhpMyAdmin SQL console).

I don't understand where the problem occurs. Does the UPDATE query in signup_2.php not work because of the constant error, or is it something else? The same error also appears when signup.php is executed and that code does execute the INSERT query perfectly.

This is the UPDATE query in signup_2.php:

$username = $_POST['username'];
$user_ip = $user->get_ip();
$activation_token = $_GET['token'];
// Date time to be used instead of SQL's NOW() function for security purposes
$datetime = date("Y-m-d H:i:s");

try {
    $sql = "UPDATE user 
            SET username=:username, 
                activation_token=:activation_token, 
                activation_date_time=:activation_date_time, 
                activation_status=:activation_status  
            WHERE activation_token=:current_activation_token";
    $stmt = $dbh->get_instance()->prepare($sql); 

    $stmt->bindParam(':username', $username, PDO::PARAM_STR);       
    $stmt->bindParam(':activation_token', $empty_activation_token, PDO::PARAM_STR);    
    $stmt->bindParam(':activation_date_time', $datetime, PDO::PARAM_STR);
    $stmt->bindParam(':activation_status', $active_status, PDO::PARAM_STR); 
    $stmt->bindParam(':current_activation_token', $activation_token, PDO::PARAM_STR);
    $stmt->execute();

    echo 'Username: ' . $username . '<br />';
    echo 'Activation token: ' . $empty_activation_token . '<br />';
    echo 'Activation date time: ' . $datetime . '<br />';
    echo 'Activation status: ' . $active_status . '<br />';
    echo 'Current activation token: ' . $activation_token . '<br />';
} 
catch(PDOException $e) {
    echo $e;
} 

All the echo's in the try block are executed and no exception is thrown, but the query just doesn't seem to do anything. I've double checked all the values and when executed in PhpMyAdmin it works.

So what's going on here? I just can't find the problem to execute a simple UPDATE query.

Removing the constant in signup.php and signup_2.php produces the following error:

Use of undefined constant __ROOT__ - assumed '__ROOT__' in signup.php...

I've tried everything I know.

Upvotes: 1

Views: 8504

Answers (2)

Faiz Rasool
Faiz Rasool

Reputation: 1379

Your User.class.php class already have

define('__ROOT__', dirname(dirname(__FILE__))); 

remove this from signup.php because you are importing User.class.php and define is kind of constant value cannot be changed once set.


Other Fix Create new file call Constants.php

define('__ROOT__', dirname(dirname(__FILE__)));

now top of every file

include_once 'Constants.php'; 

Upvotes: 4

Mohamed Belal
Mohamed Belal

Reputation: 622

Remove the define('__ROOT__') from User.class.php, you do it before you require User class and the class define it again so you get error already defined

Upvotes: 1

Related Questions