PHP - Good (best) Ways To Lean Php....
I would realy like to study php, other than taking other codes and editing them i havnt got a clue.
Similar TutorialsIs there a shorter way to achieve this in a single foreach()? foreach ($_SESSION['cart']['content']['sizes'] as $content); foreach ($_SESSION['cart']['content']['sizem'] as $content); foreach ($_SESSION['cart']['content']['sizel'] as $content); I am thinking something like: foreach ($_SESSION['cart']['content']['sizes'] && $_SESSION['cart']['content']['sizem'] && $_SESSION['cart']['content']['sizel'] as $content); or foreach ($_SESSION['cart']['content'] = ['sizes'] || ['sizem'] || ['sizel'] as $content); Hello,
I've noticed that there are services that I can pay to have my code
checked for possibly unsafe / insecure code.
But I'd rather audit the code myself, as my code is not meant to make money.
Is there a list of safe ways to use PHP?
Also is there any automatic way to do this that is free?
For instance is there a code checker?
I've noticed there are a number of ways to do PHP wrongly
that can be easy to overlook. Is there a list of common PHP pitfalls?
Thanks.
Hi all If there a better way of setting variables within classes than taking it through the __construct and setting via $this ?? e.g. class SectionsConnect { protected $var; public function __construct($var){ $this->var= $var; } } Can you not set the variable automatically as it comes in through the __construct? Thanks Magnetica What would be the simplest way? Do you use a method in the child class? Personally I use Code: [Select] parent::$this->whatever; But I was wondering what you guys do. Hello, mates! I am new to PHP. I want to store some data shared between requests. I know that it is possible with memcached and shared memory in Unix. What ways to do it could you advice? How could I realize background service in PHP? Thank you.
Is this an acceptable way to instantiate classes?
//$_SESSION['product'] = {'ProductA', 'ProductB', ... 'ProductX'} $p = new $_SESSION['product'](); $p->save();I am usually used to calling out classes explicitly where class name is not a variable but a hardcoded string. Sometimes I use if/then/else in order to do this. Here it is a variable and it bothers me a little bit. But PHP allows me to do this. Is this an acceptable latest & gratest modern PHP object oriented web technology technique or not ? Ok. I know you can pass the object of a class as an argument. Example: class A { function test() { echo "This is TEST from class A"; } } class B { function __construct( $obj ) { $this->a = $obj; } function test() { $this->a->test(); } } Then you could do: $a = new A(); $b = new B($a); Ok so that's one way i know of. I also thought that you could make a method static, and do this: (assuming class A's test is 'static') class B { function test() { A::test(); } } But that is not working. I'd like to know all possible ways of accomplishing this. Any hints are appreciated. thanks I was wondering if this is faster or better performance. Each time a user submit's a post on my forum I will make it +1 the .txt file on my main server directory called test.txt, then I can just call that on my main forum index to show the amount of total posts our board has, instead of using mysql queries to sum all of it from the tables... good idea? or am i retarded? I have a SQL statement which is difficult to use PDO on, it might not even be possible to do.
So I'm filtering it like this:
$search = $_GET['search']; $search = preg_replace("/[^A-Za-z0-9]/", " ", $search); $search = $mysqli->real_escape_string($search);Will this result in an acceptable level of security? Edited by anderson_catchme, 16 September 2014 - 12:28 PM. Hey! I was wondering if anyone had a really nice login tutorial that teach to make a secure login that is easy to use. Also, it would be best if it checked the other files as a normal logincheck.php does, but it gotta fit the login of course Anyone ? hello; I have: mysql: utf8_general_ci index.php: header( ... UTF-8) index.php: <meta ... content-type ... UTF-8> index.php, mysql query procedu ... mysql_set_charset( utf8 , .. ) ... So, if I put a special character in my db, it WILL display correctly in index.php But, if I put the same character in a php variable, it is BAD ... diamond-shape with question mark inside Since my special characters work from the db, I would like to also use them in html (index.php ). am I missing anything? thanks for your time .. Shannon I just made this mini class for hashing passwords, is this all there is to it? Setting a salt string, and hashing the string using something like sha1(md5($salt.$password)) Code: [Select] <?php class MyHash { private $salt = "a6B2yj90sZ34"; public function set_salt($salt){ $this->salt = $salt; } public function hash_string($string){ return sha1(md5($this->salt.$string)); } public function check_hashed_string($user_input, $correct_pass){ if($this->hash_string($user_input) == $correct_pass){ return true; }else{ return false; } } } ?> Code: [Select] <?php /* * To change this template, choose Tools | Templates * and open the template in the editor. */ $width=120; $height=60; $image= imagecreate($width, $height); $txt=substr(md5(rand()),0,8); $white= imagecolorallocate($image, 255, 255, 255); $black= imagecolorallocate($image, 0, 0, 0); $red= imagecolorallocate($image, 255, 0, 0); $new= imagecolorallocate($image, 128, 128, 255); //imagefilledrectangle($image, 10, 10, 110, 50, $white); imagefilledrectangle($image, 20, 20, $width-20, $height-20, $new); imagestring($image, 40, 27, 22, $txt, $black); header("content-type:image/jpeg"); imagejpeg($image); imagedestroy($image); ?> Can someone provide me with a good tutorial on how to create really nice looking buttons please. Not just crap ones, like real nice ones. XD Thanks in advance Let's say for an "entry-income or low-income" to get started with.
How should it look like when it comes to the skills?
Could it work out if somebody for example can program with "bind parameters" and OOP in PHP?
What are your recommendations?
Edited by glassfish, 26 October 2014 - 10:20 AM. Hi there, I am currently using the in_array() function to check the user's country with the country in the ShippingProfile table. I have a function that returns the shipment profile info from the table. $myshipProfile = filter_shippingprofile(array(......)); And in the $myshipProfile I get the profile info with the countryID. Now I can check the user's country in the array: if(in_array($_SESSION['userCountryId'], $myshipProfile)) { print "yes country is found"; } I works fine so far. But there is a slight problem to it. I did a quick: print_r($myshipProfile); And the output was: Array ( [shippingprofileID] => 14 [supplierId] => 66 [shippingprofilename] => ProfName1 [shippingto] => CustomCountries [ShipRegionCountries] => 66 [shippingcost] => 10 Notice the supplierId and the ShipRegionCountries has the same value. And now when I try to perform in_array() it will return true every time even if the ShipRegionCountries is not 66 because as supplierID is 66. You see the conflict. Therefore, can we write like this: if(in_array($_SESSION['userCountryId'], $myshipProfile['ShipRegionCountries'])) { print "yes country is found"; } I get no ouput for this. Whats the correct way to check only the 'ShipRegionCountries' in the array? Thank you I'm going to write a script that determines if a proxy is good or not through cURL and I would like to know if anyone knows what qualifies a proxy as being good. Let's assume I need to do some surfing through a proxy - that's it. Is there a way in PHP to determine the 'type' of proxy? For example (elite, codeen, etc.) If the proxy page exists is that all I need to run cURL through it or otherwise consider it as being good? Or should I focus my attention towards simply going through the whole process of getting a 'dummy' page using cURL through the proxy and, should it succeed, it will be considered good. I suppose if I can avoid the latter then the script would be more efficient. Advice and suggestions are always greatly appreciated here. This topic has been moved to Other Programming Languages. http://www.phpfreaks.com/forums/index.php?topic=347880.0 This topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=347419.0 I've always had an issue needing to write and maintain script to validate both client side and sever side so a while back I wrote a validation class whose primary purpose was: Accept in its constructor a JSON string which is mostly identical to the jQuery validation option except it also included a "sanitize" property. For instance: {"rules": {"account": {"minlength": 6}}, "messages": {"account": {"minlength": "Six numbers are required"}, "sanitize":{"account":int}}. Provide the correct jQuery validation option so a form can be validated client side. Sanitize the form data server side. Validate the form data server side.I recently looked at the code and it is a train wreck. I used a single class and had a bunch of protected functions where some would validate per a given rule and return the message upon error and others would sanitize. For non-typical rules required by the specific application, I would extend the class and add them. Some of these added rules required other resources such as an entities primary key, a PDO connection, etc. While inheritance worked fine to add simple rules, it did not do so for the rules which needed other resources. Also, even though I used "final" on my base class methods, I never liked this single class of many methods as I couldn't use the same name for a given rule and sanitize method (i.e. would rather use digit and digit instead of digit and int). I am thinking of changing it to something like the following. While this is definitely better, I question whether I should be using traditional classes to define my standard and custom rules. Any recommendations? Thanks class Validator { protected $options, $rules=[ "digit"=>function($value, $prop, $name){ return /*validate and return error string or null*/; }, "minlength"=>"ect, etc" ]; public function __construct(string $json, \Closure ...$customRules=[]) { $this->options=json_decode($json); $this->rules=array_merge($this->rules, $customRules); } } class Application { public function someMethod() { $pdo=$this->pdo; $applicationCustomRules=[ 'someCustomRule'=>function($value, $prop, $name) use ($pdo) { return /*validate and return error string or null*/; } ]; $validator=new Validator($json, ...$applicationCustomRules); //Which can be used such as: $jQueryOptions=$validator->getJQueryOptions(); $validator->validate($data); $validator->validateProperty('account', $account); } }
|