Sign in to follow this  
Philip Borgstr

Bad php code

Recommended Posts

Philip Borgstr    338
Hello

I am very new to PHP development and I am currently learning java and php at the same time with integration and I therefor mix them together with the syntax and so. This current code is meant to use an passed in arguments using $_GET and then process them. The current solution is just for temporal development focusing on the client java side. The problem is that not even my temporary solution is working and my IDE does not tell me any errors. I think I messed up with the scope but I am not sure.

[source lang="php"]


//Set mode
$error = NULL;
$output = NULL;
try{
$mode = $_GET["mode"];
}catch(exception $exc){
$error = "No mode";
}

//set error to null, results in message to error if set to true

//Check to make sure it is an valid user
if ($mode == "userVerify"){
if (username == "open"){
$output = "valid";
}else{
try{
$username = $_GET["username"];
$password = $_GET["password"];
}catch(exception $exc){
$error = "No username or password given";
}
$output = "valid";
}
}

//Connection checking, allways returns true if executed
elseif ($mode == "connectionCheck"){
$output = "true";
}

//Check prestige level
elseif ($mode == "userPrestigeLevel"){
$username = $_GET["username"];
if (username == "open"){
$output = "0";
}else{
//Executed if it is not an open user
$output = "0";
}
}

else{$error = "Unvalid mode" . $mode;}

if ($output == NULL){
$error = "No output";
}

if ($error != NULL){
$output = "ERROR: " . $error;
}
echo $output;


[/source]

I pass in arguments using the sytem localhost/file.php?mode=userVerify?username=test?password=test and all I receive back is "ERROR: No output". I know this code is anything but pretty, but for know all i want is some basic functionality. Edited by philipborg

Share this post


Link to post
Share on other sites
demonkoryu    980
Also, accessing non-existing array indices doesn't throw an exception. You should check for existence of these parameters by using [i]isset()[/i].

[source lang="php"]
if (isset($_GET['mode']) {
$mode = $_GET['mode'];
} else {
$error = "Required param 'mode' is not set.";
}
[/source] Edited by demonkoryu

Share this post


Link to post
Share on other sites
slicer4ever    6760
just tossing this out their(you might be already planning this, I don't know), but ontop of using POST to pass variables, i'd recommend hashing the password on the client side, before submitting it to your server, again you might have this already in mind, but just making sure is all=-)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this