Jump to content

Drongo_III

Members
  • Posts

    579
  • Joined

  • Last visited

Posts posted by Drongo_III

  1. Hi Guys

     

    I don't know whether this is possible or not but I've been going down a lot of dead ends for a few hours now and I'm ready to defer to a higher power...

     

    I want to create a function that will accept an array of keys as it's first argument and a value as it's second argument and basically add the keys in a multidimensional format to produce an array with the data as its final value.

     

    So if I passed into the function:

    someFunc(array('key1','key2'), 'data');
    
    

    I would end up with an array that looks like: [key1][key2] = 'data'

     

    I've been trying to do it with a while loop but I become unstuck in adding the subsequent layers to the array. I've also explored using the 'end' and 'key'  method hoping I could just specify the pointer and add an element there but there doesnt appear to be anything like that.  Any help is very welcome!

     

    Dronogo

     

     

     

     

     

     

  2. Yeah I could use a relative path but on other servers I've worked on they've set them up so you just use /includes/file-name.php and it doesnt matter where you then make the call from it still works.  I guess I was just trying to work out how to configure xampp so it always looks at /htdocs/ as it's root when including files?  

  3. Hello

     

    Hope someone can help and I'm sure this is a simple one.

     

     I'm using a local copy of XAMPP at the moment and have a basic issue but I've been searching around and struggling to find the answer.

     

    Usually when I work on a site, lets say it's domain is example.com, and I wish to include a file from a location like example.com/includes/, I would simply use a php's include as follows:

    <?php
    
    include('/includes/some-file.php');
    
    ?>
    

    In this instance I'd expect the '/' to refer to the base domain - i.e. example.com - so that irrespective of where I include the file from it always has a sound reference.

     

    However, on my local xampp i'm having issues.

     

    So lets say I have a file I want to include located in in:  /htdocts/ng/includes/some-file.php

     

    And I try to include it from a sub directory /htdocs/ng/some-dir/ as per the code posted above I get an error:

     

    "No such file or directory in C:\xampp\htdocs\ng\some-dir\some-file.php"

     

    It's as if the include path is always trying to include from the current directory.  So is there a setting in apache I need to change to ensure that the base domain is always referenced?

     

    Hope that makes sense,

     

    Drongo

  4. The delay is part of the keyboard and operating system. Notice how if you hold down a letter in a text box, you get the same delay? That delay occurs on the keydown event as well.

     

    One way to fix this is to assume that the key is held down until a keyup event occurs. So you press a key, it fires a keydown event and repeats that action until a keyup event occurs for the same key, which then stops the event. Does that make sense?

     

    Scoot you're a genius sir! I hadn't considered this was symptomatic of the way the OS handles repeating keys.  Your post got me to searching out how to overcome the delay and I found a solution along the lines of what you proposed. Essentially you have a loop monitoring a keyState object to determine whether a key is up or down and then it reacts accordingly. The solution also means multiple key presses can be used at once.

     

    The code is below if anyone experiences the same problem:

    <!doctype html>
    <html>
    <head>
    
    <style type="text/css">
    
    canvas {
    
    	border: 1px solid #888;
    	
    }
    
    </style>
    
    <script>
    
    window.onload = function(){
    	//initialise canvas and context
    	c.init();
    	
    	window.addEventListener('keydown', function(e){
    		keyState[e.keyCode || e.which] = true;
    		
    
    	}, false);
    
    	window.addEventListener('keyup', function(e){
    		keyState[e.keyCode || e.which] = false;
    		
    	});
    		
    }
    
    //holds key state 
    var keyState = {}
    
    //canvas 
    var c = {
    	
    	cvs : null,	//canvas element
    	ctx : null, //context
    	pos : {y:0,x:0},
    	inc : 5,
    	
    	init : function(){
    		
    		//create canvas obj and context
    		this.cvs = document.getElementById('canvas1');
    		this.ctx =  this.cvs.getContext('2d');
    		
    		//draw initial rectangle at starting point
    		 c.ctx.fillStyle = 'black';
    		 c.ctx.fillRect(0, 0, 60, 60);
    		 
    		 //start keystroke monitoring
    		 this.loopPosition();
    	},
    	
    	
    	
    	animate: function(){
    		
    		//clear the canvas
    		this.clearContext();
    		
    		//redraw in new position
    		this.ctx.fillStyle = 'black';
    		this.ctx.fillRect(this.pos.x, this.pos.y, 60, 60);
    	},
    	
    	clearContext : function(){
    		c.ctx.clearRect(0,0,this.cvs.width,this.cvs.height);
    	},
    	
    	
    	loopPosition: function(){
    		if(keyState[37]){
    			this.pos.x-=1;
    		}
    		if(keyState[39]){
    			this.pos.x+=1;
    		}
    		if(keyState[40]){ //down
    			this.pos.y+=1;
    		}
    		if(keyState[38]){ //up
    			this.pos.y-=1;
    		}
    	
    	
    		//then animate 
    		this.animate();	
    		
    		//loop the method
    		setTimeout(this.loopPosition.bind(c),10);
    			
    	}
    
    } 
    
    </script>
    
    </head>
    <body>
    <canvas id="canvas1" width="500" height="500"></canvas>
    
    </body>
    </html>
    
  5. Hi

     

    I'm testing out animation using canvas..

     

    The code below just animates a square in response to holding down one of the arrow keys.  

     

    The problem is when I hold down an arrow key there appears to be a short delay before the square starts animating.  I'm guessing it's something to do with the keydown event firing rapidly but I'm not sure.  

     

    So if anyone has any suggestions as to why this might be happening it would help me greatly!  The desired effect is for the square to animate immediately without any delay.

     

    Thanks,

     

    Drongo

    <!doctype html>
    <html>
    <head>
    
    <style type="text/css">
    
    canvas {
    	border: 1px solid #888;
    }
    
    </style>
    
    <script>
    
    window.onload = function(){
    	//initialise canvas and context
    	c.init();
    	
    	//set event handler for keydown
    	window.addEventListener('keydown', c.setPosition.bind(c));
    }
    
    
    
    //canvas 
    var c = {
    	
    	cvs : null,	//canvas element
    	ctx : null, //context
    	pos : {y:0,x:0},
    	inc : 5,
    	
    	init : function(){
    		
    		//create canvas obj and context
    		this.cvs = document.getElementById('canvas1');
    		this.ctx =  this.cvs.getContext('2d');
    		
    		//draw initial rectangle at starting point
    		 c.ctx.fillStyle = 'black';
    		 c.ctx.fillRect(0, 0, 60, 60);
    	},
    	
    	
    	
    	animate: function(){
    		
    		//clear the canvas
    		this.clearContext();
    		
    		//redraw in new position
    		this.ctx.fillStyle = 'black';
    		this.ctx.fillRect(this.pos.x, this.pos.y, 60, 60);
    	},
    	
    	clearContext : function(){
    		c.ctx.clearRect(0,0,this.cvs.width,this.cvs.height);
    	},
    	
    	setPosition : function(e){
    		
    		//increment position based on key
    		console.log(e.keyCode);
    		switch(e.keyCode){
    			case 40:
    			this.pos.y += this.inc;
    			break;
    			
    			case 38:
    			this.pos.y -= this.inc;
    			break;
    			
    			case 37:
    			this.pos.x -= this.inc;
    			break;
    			
    			case 39:
    			this.pos.x += this.inc;
    			break;
    		}
    		
    		//then animate 
    		this.animate();	
    	}
    
    } 
    </script>
    
    </head>
    <body>
    <canvas id="canvas1" width="500" height="500"></canvas>
    
    </body>
    </html>
    
  6. Hello

     

    Probably a really basic answer here but I'm stuck and google isn't bringing me any closer.

     

     

    Is it possible to dynamically create objects using a variable?

     

    For instance in PHP I might do:

    class turnip {
    	
    	public function __construct(){
    		echo 'created turnip';
    		
    	}
    	
    }
    
    $t = 'turnip';
    $tt = new $t(); //echoes created turnip
    

    However, trying to do something similar in JS is simply causing an error.  So is there a special trick?

     

    Lets say I wanted to do:

    function Pawn(){...}
    
    var p = 'Pawn';
    
    var a = new p(); //Can this be done somehow? Can you force the variable 'p' to evaluate down to it's value?
    
  7. Hi Guys

     

    I'm just wondering whether there's a more elegant way to access the event object in the following example.

     

    I realise I could use jquery's $.proxy method but lets say we hypothetically had to use call() it doesn't seem very clean to have to pass 'e' into the anonymous function and as a parameter on call().  

     

    So is there a better way to access the event object in this scenario?

     

    I know this is a bit of a pie in the sky question but I'm just keen to understand whether what follows is the only way or not.

     

    Thanks,

     

    Drongo

    	$(document).ready(function(){		
    		$('#tt').on('click', function(e){			
    			test.init.call(test,e,'some text as extra param');
    		});
    	});
    	
    	var test = {
    		
    		init: function(e, g){		
    			e.preventDefault();
    			console.log(e);
    			console.log(g);		
    		}		
    	}
    
  8. Thanks requinix. I though gd library wasn't the best idea but you'd be surprised how many websites recommend it.

     

    I wouldnt have thought of base64 - really nice suggestion. :)

  9. I'm currently planning out a pixel tracking PHP script. 

     

    From reading around there appears to be two ways to serve a pixel back to in the request:

     

    1) creating a 1x1 pixel image using GD library

    2) sending back 200 code in the header and serving no image at all

     

    However, using GD library seems like quite an expensive way to serve a 1x1 pixel. So what I wanted to know was if I went down this route is there any advantage of using GD library over just serving a 1x1 pixel image (i.e. a 'physical' image) that's actually stored on the server?

     

    Any advice is appreciated!

     

  10. Firstly I wouldn't rely on client side JavaScript alone for verifying a password - you'll want to validate server side too.

     

    In order to check for the presence of uppercase/lowercase/number you can use a function something similar to the one I've jotted below. This uses a simple regular expression to test a password string against different patterns.

     

     You can easily adapt this to also count the number of instances of a particular character as the 'result' variable will get populated with each individual instance of either an uppercase letter, lowercase letter or number. So you can check result.length to see if the password provided meets your criteria.

     

    Hope this helps get you on the right path.

    
    <script>
    
    function testPassword(type, password){
    
    var result,
    patterns = {
    uppercase : /[A-Z]{1,1}/g,
    lowercase : /[a-z]{1,1}/g,
    number : /[0-9]{1,1}/g,
    }
    
    //pass value of match to result - it becomes null if no match is found
    result = password.match(patterns[type]);
    
    //return true if a match is found or false if not
    return result === null ? false : true ;
    
    }
    
    var password = "AAcc";
    
    console.log( testPassword('uppercase', password) ); //returns true as the password string has caps
    console.log( testPassword('lowercase', password) ); //returns true as there are lowercase characters
    console.log( testPassword('number', password) );  //returns false - no digits in the password str
    
    
    
    </script>
    
  11. Hi Guys

     

    I'm not entirely sure this is the right section but I need a little advice.

     

    Three quick questions:

     

    1) Can you force caching on a script (js) that's loaded from an external source over which you have no direct control. So lets say you have a script being loaded on a page async. - i.e. via createNewElement...  Can you force that script to cache?

     

    2) This may actually answer question one -  is an external script served under the headers of the source server?  So if that server had caching set on all script files and I incorporated a script from that server into a page on my server would that file be subject to the source servers caching?

     

    3) If point two is the case - then does that count for all files incorporated from an external source?

     

    Thanks,

     

    Drongo

  12. Thanks Ch0

     

    That worked a treat.  I've just re-read some tutorials on positive and negative lookaheads but something is confusing me.

     

    If I had a pattern like this:

    '/^([a-z0-9_\-\.]+)(?!\.jpg)$/i';
    

    Why is it that preg_match matches the whole string (i.e. including the .jpg)? Doesn't that pattern mean "match alphabetical, numeric, underscore, dash and full stop BUT don't match if the string ends in .jpg"?  So by my understanding, which appears to be wrong, that shouldn't have matched anything.

     

    Thanks,

     

    Drongo

  13. Hello

     

    So I want to try and match everything preceding a file extension in a string using preg_match.

     

     

    An example file name:  "Images_home_blah.blah.jpg" . 

     

    I've tried the following regex:

    /^([a-z0-9_\-\.]+)(?!\.jpg)/i
    

    But this sadly appears to capture the whole string instead of ignoring the '.jpg' part.  Can anyone point me in the right direction?

     

    Thanks,

     

    Drongo

  14. Hi Guys

     

    I'm building a php application where users must login to access a set of private images. The images display as a set of thumbs to the user, which they can click to download the full size image. so I need to read a whole batch images (500+) of them from a directory.

     

    So I'd like some advice on the best way to:

     

    1) Keep files from being world readable.
    I know you could probably achieve this using htaccess or storing them outside the document root but which is best given that I need to read a whole directory with hundreds of images?

    2) How to access the images

    If it turns out storing the images off document root is the way to go, what's the most efficient way to read a directory off the doc root and access the individual images (readfile for instance)?

     

    Thanks in advance,

     

    Drongo

     

  15. Hello

     

    This may seem like a silly question but if you never ask you never know...

     

    Lets say I json encode a php array. When I echo out the encoded json string into a JS variable it looks something like this in the resultant markup:

    var arr = ["cow - Copy (10).jpg","cow - Copy (2).jpg"];
    
    

    So in this instance the string I've echoed out is equivalent to a JS array and I can use it straight away.

     

    My questions:

     

    1) Is it valid to do what I've done above?

    2) Since I can use the array right away is there any need to JSON.parse?

    3) When would you use JSON.parse?

     

    Thanks,

     

    Drongo

  16. SCRATCH THAT! - had a typo... ;D:suicide:

     

     

    Hi Guys

    I have another SQL question and I hope someone can help.

    I'm trying to run a query with two 'IN' statements.  The first IN statement is compiled from a sub-query and the second is hard coded (its from an external list, which is actually much larger than in the example below)

    I keep getting an sql error when I run this through PHP MyAdmin but the error isn't giving me much to go on and I'm not even sure if it's valid to run two IN statements in the same query.  Any help on how I might achieve the above would be most welcome!

    Oh and postNumber is an integer representing posts but there are some duplicates in the database hence the DISTINCT query

    SELECT * FROM `posts` WHERE `postNumber` IN (SELECT DISTINCT(`postNumber`) FROM `post` WHERE `postYear` >2013) OR `postNumber` IN ('10088','9813','7991')
    

    Drongo

  17. i suspect you actually want to find the matching row(s) with the highest version number (i.e. you want the latest content, not just the highest version number.)

     

    see this link - http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.html

     

     

    I'm still confused.

     

    So if I wish to select all records that are like a particular keyword, but in instances where there are multiple records on the same postID only return the highest version number, would I do something like this?

    
    SELECT * FROM table t1 WHERE title LIKE '%SOME-TITLE%' AND version = (SELECT MAX(version) FROM table t2 WHERE title LIKE '%SOME-TITLE%');
    

    I don't have the database in front of me at the moment to test it although I'm not sure I quite understand how to construct the query for what I am trying to achieve.

  18. Hi Guys

     

    I have a table which in it's shortened form has the following columns:

     

    id  |  postID  |  title  |  content  |  version

     

    The column for postID has a number that can be shared by multiple rows - differentiated by version number.

     

    I want to run a query to select all records that are like a given keyword (i.e. %LIKE%) but where results share the same postID I only want to return the highest version number for that record.  

     

    The difficulty is some records may have multiple version numbers that match the like statement and some may have only one. So this variance with the LIKE search is causing me some confusion.

     

    I've tried this in a few ways using a sub-query but for the life of me I cannot work out how to do it.

     

    Any help would be appreciated,

     

    Drongo

     

  19. Thanks Requqinix - your advice is much appreciated.  I will use the OR flag then to not complicate things :)

     

     

    I can't really fix the injection for this xss attack as it's based on someone appending to a valid url. So I'm not sure how I've thwart that.

     

    I have another post on here where it's all explained: http://forums.phpfreaks.com/topic/292145-reflected-xss/

     

    I was looking to use x-content-security-policy header but I've since read that trying to recognise certain patterns in the url is also a way to go.

     

    How would you recommend getting around an xss where someone appends something like this to the url?

    %22%3E%3Cimg%20src%3da%20onerror%3dalert(1)%3E6f54e?sub=t
    

    Sorry btw - don't mean to start covering things from other threads but as it came up...

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.