PHP failing to check conditions - php

im working on a script that shows Minecraft faces based on URL, but i got a problem, everytime an invalid username is inputed, it saves it into the skins folder, a thing it should only do when when a username IS valid.
full script:
Sorry, the problem has been already solved, i'm not going to give my script away.
It's quite long i know, but it's the full script.
So i remind, the problem is that the script saves invalid usernames too, a thing it should only do if a username is vaild.

Try to add exit of wrap all other code in else{ } statement
if(!$src){
$src = imagecreatefrompng("http://s3.amazonaws.com/MinecraftSkins/char.png");
//here's where my problem, i am trying to only display the image on screen if not found, but for some reason it saves it into the skins folder
imagepng($final);
**exit();**
}

You can use a pre-existing api made by minotar; To get my skin "Phyore" You use https://minotar.net/avatar/Phyore when Minotar doesn't recognise the username (Doesn't exist) it'll give you the steve head.
$user = 'Phyore';
//Size defaults to 180 x 180
$size = '100';
echo '<img src="https://minotar.net/avatar/'.$user.'/'.$size.'"';
See more at https://minotar.net/ They even do full skins.

Related

Laravel3: How to check if there is a file uploaded

I have a page with a form where user can upload an image to replace his existing avatar.
However, if the user submits forms without uploading an image, I will not update his avatar and user can keep his old avatar.
This is an UPDATE issue so I need something like this in pseudo code:
if (Input::has_uploaded_file() === true)
//User uploaded something, update avatar column/remove old avatar etc.
else
//User didn't upload anything so don't update avatar column
I just need the first line, though.
Can anybody help? I couldn't find much about this in documentation.
If Input::has_file('name') does not work for you then you can use the equivalent of what it is doing, like this...
if (Input::file('name.tmp_name', '') != '')
Taken from laravel/input.php
I read laravel source code, i think its this:
Input::has_file('input_name');
If that's not work, try using native php:
$_FILES['input_name']['error'] = UPLOAD_ERR_NO_FILE;
Or same above with laravel:
$i = Input::file('input_name');
$i['error'] = UPLOAD_ERR_NO_FILE;
Code above is to check if user leave the upload form empty.

Can't get the upload form in CodeIgniter to work properly

I have a form where a user can edit their public profile and upload a picture in the process. Everything works fine except for when a user has a photo uploaded or just leaves it blank to not change it it still goes ahead and tries to upload it and throws an error if it's empty... I do have a check in place, however it just ignores it.
if(isset($_FILES['userfile']))
{
$file_name = $this->session->userdata('user_id');
$img = $this->custom_lib->img_upload('./upload/user/', $file_name);
$user_pic = 'upload/user/'.$file_name.$img['file_ext'];
} else {
$user_pic = 'upload/default.png';
}
The problem I think is within the if statement however I don't see what it can possibly be. If I do select an image it uploads it just fine and everything works well. If I leave the image field blank (not select a file) instead of doing this:
$user_pic = 'upload/default.png';
it tries to upload a file... which doesn't exist... and then it throws an error. The img_upload() function is inside a custom library I made, however that function shouldn't even be called if no file was submitted.
I appreciate any help!
isset will return true even if the item you are checking contains nothing, is null, or is false.
To avoid this issue, you use empty like so:
if(!empty($_FILES['userfile']))
This will only return true if the $_FILES['userfile'] contains a value that is not null, false or empty.
Hope that helps :-).

I need help getting recaptcha to work

I recenlty had a site designed for me, but my dev used a really crappy generic captcha that fails half the time. I'm trying to replace it using a recaptcha, but I'm having trouble. I cannot figure out which *.php is used for 'processing' and which is used for the 'form'.
I didn't want to post the whole code, so here it is:
This is the 'form' page, as it has the form fields and etc embedded:
http://dl.dropbox.com/u/45666699/formcode.txt
Can someone please take a look at this code and tell me where I should put the private code for recaptcha? Also, how do I disable the "random_number" captcha that is already installed? Thanks!
the code for your existing captcha is on line 295, 296 and 297
require_once('recaptchalib.php');
$publickey = "6LfIUdISAAAAAKguxgdPCjZ6-OkVeu5tmcBaa7ug"; // you got this from the signup page
echo recaptcha_get_html($publickey);
Well you'll need the private key when you're trying to validate that the correct captch was entered (i.e. at the point where you're handling the form submission)
Which by looking at your code should start immediately after line 4
Using a project i did a while back, you would have something like so...
$recaptcha_error = NULL;
//set it to NULL initially
if(isset($_POST["btnsend"])){
include_once(INCLUDES_FOLDER."recaptcha-php-1.11/recaptchalib.php");
$resp = recaptcha_check_answer(RECAPTCHA_PRIVATE_KEY,$_SERVER["REMOTE_ADDR"],$_POST["recaptcha_challenge_field"],$_POST["recaptcha_response_field"]);
if($resp->is_valid){
//captch was gotten correctly
//continue with your normal code processing here
} else {
//wrong input -- captch was invalid -- give the person the error response
//mine is as below -- my usual way :)
$response = array(array("Something seems to be wrong with the captcha!","Please check that you entered it correctly or check the returned error message"),false,"w");
$recaptcha_error = $resp->error;
//make sure to do the above so u can use it when generating the captcha display
}
}
//You got the recaptch error (or left it as NULL above so you could do this)
//when generating your captch display as done on your lines 295, 296, 297
include_once(INCLUDES_FOLDER."recaptcha-php-1.11/recaptchalib.php");
echo recaptcha_get_html(RECAPTCHA_PUBLIC_KEY,$recaptcha_error);
Hope this helps (even if a little) :)
Cheers

Protect a generate image with php

I'm having a problem here and I think people here can help me.
I have a file that generates an image, ler.php, and the file that loads the images through a while, carregar.php.
I need to block direct access to the images generated by ler.php, tried to make a system like this session:
carregar.php:
<?
$_session['a'] = 1;
while($a != 50) { echo "<img src='ler.php?imagem=$a'>"; $a++; }
$_session['a'] = 0;
?>
ler.php:
<? if($_session['a'] == 1) { //load image } ?>
The result is the only loading the first image.
I'm trying to now use the $_SERVER ["PHP_SELF"], placing the IF of ler.php, what happens is I load it through <img src=''> she identifies as carregar.php.
Who has the best solution?
I've tried several ways with $_SESSION and it seems to not really work.
I could suggest two ways:
Easy to implement, less protective: in ler.php check that $_SERVER["HTTP_REFERER"] refers to "carregar.php".
A little bit more complicated: in carregar.php generate an unique code for each image you're going to output and store it in $_SESSION. Then pass the code to ler.php as a GET parameter. In ler.php check if the code exists in $_SESSION object, then generate an image and remove the code from $_SESSION.
I have a hard time identifying the problem, but your use of the session is going to lead to unexpected results:
You are adding 50 (I guess...) image tags to a page and right after you have added these tags, you set the session variable to 0.
The browser only loads a few files from the same server at the same time, so when the script is done and the first image is loaded, the browser is going to request the next image but that will fail as you have set the session variable to 0 already.
The only way to reliably set the session variable to 0 after all images have loaded, is an ajax request from your page that checks and triggers after all images have completely loaded.
Good! I managed to resolve one way and improvised without using AJAX:
ler.php:
<?php
if(isset($_SERVER["HTTP_REFERER"])) {
$check2 = (strpos($_SERVER["HTTP_REFERER"], 'carregar.php') > 0) ? true : false;
if(print_r($check2) != 11) {
// Blank
}
} else {
if(isset($_SERVER["HTTP_REFERER"]))
{
// Load Image
}
if(!isset($_SERVER["HTTP_REFERER"]))
{
// Blank
}
?>
So, the image only can be loaded into my page. Maybe...

Open txt files from a directory, compare the values, and output the top 15 in PHP

I recently designed a referral game website for the fun of it.
There's a simple MySQL user system with a email verification. It's using the UserCake user management system.
On top of this i added a php page that the user could give to "victims" that when they visit it they get "infected" and can infect other users or "victims". This page uses GET to get the username from the url. I have a folder that when a user registers it creates a file with 4 digits and then the username. (ex; 0000Username.txt) All the numbers are the same, it's just so that if a user discovers the folder they won't be able to find the files. There is also a txt file in the same format with IPS in the name. (ex; 0000IPSUsername.txt) The file when visited gets the username from the url, then checks if the text file for that username exists. If the username is present in the url, and a valid user it opens the IPS file and adds the IP of the visitor, then opens the user text file, takes the value and adds one to it, and saves. At the end it makes the difference between saying "You are infected, Username has infected (amount) people." or just you have been infected.
Now to what i need!
I need to add a hi-scores to the website so people can compete to be the one with the most "infections".
I thought i could use readdir to get a list of the files and open them with the value in an array, but i need it to also strip the username from the file name. It would be best if it just saves to a text file like "Username | value" because then i can add echo's of the html tags and have it include the file in the page i want it to be one.
Many thanks in advance.
Try using an array and count($array) to count the files.
Stripping the values out is as simple as using PHP's str_replace function. More here: http://php.net/manual/en/function.str-replace.php
With the 0000Username.txt file try using json.
// Get
$contents = file_get_contents('0000Username.txt');
$data = json_decode($contents); // PHP > 5.2.0
// Put
$data['username'] = 'Username';
$data['infectious'] = $data['infectious'] + 1;
$contents = json_encode($data); // PHP > 5.2.0
file_put_contents($contents);

Categories