Page 1 of 1

function return value in write context

Posted: Mon Jul 25, 2011 3:03 pm
by bowersbros
Im confused by this because I have managed to have this work before in the past,


Here is my code:
<?php
	require("functions.php");
	
	$_SESSION['errors'] = NULL;
	if(isset($_POST['submit'])){
		$username = $_POST['username'];
		$password1 = $_POST['password'];
		$password2 = $_POST['password2'];
		$fname = $_POST['firstn'];
		$lname = $_POST['lastn'];
		$gender = $_POST['gender'];
		
		$errors[] = array();
		if(empty(trim($username))){
			$errors[] = "Please enter a username";
		}
		if(empty(trim($password1))){
			$errors[] = "Please enter a password";
		}
		if(empty(trim($password2))){
			$errors[] = "Please confirm your password";
		}
		if(empty(trim($fname))){
			$errors[] = "Please enter your first name";
		}
		if(empty(trim($lname))){
			$errors[] = "Please enter your last name";
		}
		if($gender == "default"){
			$errors[] = "Please select your gender";
		}
		if($password1 != $password2){
			$errors[] = "Your passwords do not match";
		}
		$query = mysql_query("SELECT COUNT(`user_id`) as `id_exists` FROM `users` WHERE `username` = '".$username."'");
		if(mysql_num_rows($query) != 0){
			$errors[] = "Username already exists";
		}
		if(isset($username[5])){
			$errors[] = "Your username needs to be 6 characters or more";
		}
		if(isset($username[31])){
			$errors[] = "Your username is too long";
		}
		if(isset($fname[31])){
			$errors[] = "Your first name is too long";
		}
		if(isset($lname[31])){
			$errors[] = "Your last name is too long";
		}
		if(empty($errors[])){
			$username = mysql_real_escape_string(strtolower($username));
			$password2 = md5(add_salt($password2));
			$fname = mysql_real_escape_string(strtolower($fname)); 
			$lname = mysql_real_escape_string(strtolower($lname));
			if(($gender == "male") || ($gender == "female")){
				$gender = $gender;
				// set male to m, female to f
			} else {
				header("Location: index.php");
			}
			$query1 = mysql_query("INSERT INTO users (`user_id`,`fname`,`lname`,`gender`,`passwd`,`profile_pic`,`username`) VALUES ('','".$fname."','".$lname."','".$gender."','".$password2."','','".$username."')") or ($fail = true);
			if($fail === true){
				$errors[] = "The insert failed. Please try again later.";
				$_SESSION['errors'] = $errors[];
				header("Location: index.php?error=true");
			} else {
				header("Location: index.php?success=true");
			}
		} else {
			$_SESSION['errors'] = $errors[];
			header("Location: index.php?error=true");
		}
		
	} else {
		header("Location: index.php");	
	}
?>
Function.php:
<?php
session_start();
error_reporting(1);

mysql_connect("localhost","root","root") or die(mysql_error());
mysql_select_db("general");


function add_salt($string){
	$string .= "A4dceed";
	return $string;
} 
function is_logged_in(){
	if((isset($_SESSION['username'])) || (isset($_SESSION['uid']))){
		// return true;
	} else {
		// return false;
	}
}

?>

Re: function return value in write context

Posted: Mon Jul 25, 2011 3:09 pm
by jacek
How is anyone going to be able to help if you don’t say what the problem is !

Re: function return value in write context

Posted: Mon Jul 25, 2011 3:11 pm
by bowersbros
jacek wrote:How is anyone going to be able to help if you don’t say what the problem is !
How foolish of me, I thought i copied the error over.

bascically, it says: Fatal error: Can't use function return value in write context in /httpd.www/register.php on line 14

Which, because of the require, makes me think it is actually line 14 of the functions.php page, which is a if() statement .
if((isset($_SESSION['username'])) || (isset($_SESSION['uid']))){
also, after that, I commented out the return statements. Imagine they aren't commented as in the code I am using they are not commented.

Re: function return value in write context

Posted: Mon Jul 25, 2011 7:05 pm
by jacek
if(empty(trim($username))){
Basically empty only works on variables, so you can't do this.
bowersbros wrote:Which, because of the require, makes me think it is actually line 14 of the functions.php page
php always tells you the right file ;)

Re: function return value in write context

Posted: Mon Jul 25, 2011 9:18 pm
by bowersbros
Ah okay.

but i thought that because it is included effectively it is C+P'ed into that file?

Or is that not how it works?

And should i do instead:
$username = trim($username);
if(empty($username)) //blah
Is that how it should be done then?

Re: function return value in write context

Posted: Tue Jul 26, 2011 12:19 pm
by jacek
Yeah that is how you would have to do it to use empty() but that looks a little messy !

I assume you have somethign like
$username = mysql_real_escape_string($_POST['username']);
so you could just trim there ?