English 中文(简体)
Can t use sha1 inside class definition?
原标题:
  • 时间:2009-11-18 12:11:51
  •  标签:
  • php
  • sha1

I m working on a login class in PHP. I have the following line inside a class definition:

private $salty = sha1( salty );

...which is giving me the following error:

"Parse error: parse error, expecting , or ; in C:xampphtdocs estincludesjaLogin.php on line 26"

I ve tried using this line outside of the class definition and it works fine, it s only when I use it inside the class definition that I get a problem.

If I remove the word "private" I get a slightly different error:

Parse error: parse error, expecting `T_FUNCTION in C:xampphtdocs estincludesjaLogin.php on line 26

I feel like I m missing something obvious...

最佳回答

You can t use function-calls to initialize class member variables.

class YourClass
{
    private $salty;

    public function __construct()
    {
        $this->salty = sha1( salty );
    }
}

is the one way to initialize your variable.

EDIT

Even, e.g., a simple concatenation of two constant strings is not allowed (protected $_string = Hello . World! ;). The evaluation of class properties happens at compile time, so the usage of constructs that depend on run-time information is illegal.

[...] This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

(Properties)

问题回答

Just declare the variable as null and then initialize it in your constructor.

private $salty = null

function __construct() {
    $this->salty = sha1( salty );
}

$sha1() won t function because you re trying to handle a function as a variable.

And when you re working on a better login mechanism why not start using:

$this->salty = hash("SHA512", "salty");

Additionally you can create a salt hash and add it with your password. This addition gives you a little overhead because you will have to save the salt in a db-table too and retreive and combine it when you are validation (user)credentials.

Hope it helps you!





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签