This is the code behind the demo at https://www.daniweb.com/connect/oauth/demo

Member Avatar
+1 forum 0

You might be familiar with the dreaded blank page when your PHP script doesn't work. Here's how to spit out errors to the screen, instead of getting just a blank page, as well as logging errors to a file.

Member Avatar
+0 forum 0

There are two ways to write to a file in PHP. You can either open a stream, and write to it in parts, or you can use `file_put_contents()` which is much more convenient, with the trade off being that it takes up much more memory, and is not ideal if you want to send a *lot* of data upstream.

Member Avatar
+0 forum 0

If you need your PHP script to redirect to a different website, you can send an HTTP header to do that. Remember, `header()` must be called before any actual output is sent, which includes not just HTML, but blank lines, etc.. as well.

Member Avatar
+0 forum 0

We have a Swagger file for our API, but in our API documentation, we want to show valid responses for each endpoint. I coded up this little recursive function in PHP which takes our Swagger file and spits out a valid response. I use it as follows, for each individual endpoint: // $swagger is the entire swagger file $swagger = file_get_contents('/path-to-swagger.json'); For the below code within our PHP template, `$endpoint` can be assumed to be a single endpoint with method (e.g. a single POST request to a URI) such that json_decode() was used to convert it from the Swagger file …

Member Avatar
+0 forum 0

This code dynamically generates a clear 1x1 pixel.

Member Avatar
+0 forum 0

I recently had to mass insert a really large text file of strings into MySQL. Here's how I did it.

Member Avatar
Member Avatar
+1 forum 1

###Simple Advice This is just a reminder. When setting environment variables through **.htaccess**, **CLI** or **dotenv** use `filter_var()` to evaluate a boolean. For example, start the built-in PHP server with these variables: DEBUG=FALSE LOG=TRUE SMS=1 SMTP=0 CONNECT=yes BACKUP=no php -d variables_order=EGPCS -S localhost:8000 And then test through `boolval()`: if you forget to use 1 and 0, then something can go wrong as the variable is evaluated like a string and will always return `TRUE`. By using `filter_var()`, with the appropriate filter to validate boolean, you get more flexibility as it: > returns TRUE for "1", "true", "on" and "yes". Returns …

Member Avatar
+0 forum 0

I needed a script that would grab all the folders in the directory and add them to a dropdown list and allow the user to upload to there choosen folder. This is what i came up with. (upload script is not mine) Thought i would share.

Member Avatar
Member Avatar
+0 forum 5

I found myself starting to answer a question that I have answered many times , so I thought why not making a code snippet and just reference this. This is a short code snippet only to demonstrate how this thing work (with apache and rewrite engine on) , in real life PHP code shouldn't be under the public_html / www folder in any case (with maybe only one exception a index.php that its main task is to include something (or instantiate a class) from the src folder) Lets suppose that you have an example folder under the domain example.com , …

Member Avatar
Member Avatar
+2 forum 3

By this script you can remotely login into facebook account with your password and username and fetch/send data.

Member Avatar
Member Avatar
+2 forum 12

Export CSV TO Mysql and Import to CSV from MYsql in PHP

Member Avatar
Member Avatar
+0 forum 4

Don't you just get sick of hash algorithms constantly been brute forced or even cracked. Well here is an algorithm I have created in my spare time to help out the little guy who just wants a secure hash. This hashing algorithm uses many different techniques including ones used in sha1 to prevent a reversal algorithm and uses a lot of math to help reduce the speed of brute forcing the algorithm. An example of the brute force done on common algorithms today is like I myself have got a website to brute force sha1, sha256, sha512, md4 and md5. …

Member Avatar
Member Avatar
+1 forum 3

I've been playing with PDO and got quite a shock, so I thought I'd run this past you all. I love PDO, and I thought that the "prepare once, execute many" idea behind prepared statements was beautiful. Now previous to using prepared statements, I used to build up a long SQL 'VALUES' clause by looping through an array of "records" to insert, instead of running an INSERT query on every 'record' in the array. Sensible enough. So should I now dump that for "multiple executes" in my array loop? Well I tried. Here's my experience... I'm using a simple 'members' …

Member Avatar
Member Avatar
+4 forum 7

Here is a PHP class written for PHP4 and PHP5 that will validate email addresses by querying the SMTP (Simple Mail Transfer Protocol) server. This is meant to complement validation of the syntax of the email address, which should be used before validating the email via SMTP, which is more resource and time consuming. For more information read this thread: [URL="http://www.daniweb.com/forums/thread141944.html"]http://www.daniweb.com/forums/thread141944.html[/URL] Documentation and updates: [URL="http://onwebdevelopment.blogspot.com/2008/08/php-email-address-validation-through.html"]http://onwebdevelopment.blogspot.com/2008/08/php-email-address-validation-through.html[/URL] Source code: Source code has been moved: [URL="http://code.google.com/p/php-smtp-email-validation/"]http://code.google.com/p/php-smtp-email-validation/[/URL]

Member Avatar
Member Avatar
+0 forum 7

Hello , today I was developing a short url system and I created this class to help me encode / decode integer (id) to small string. That way for example “Ac” means 3674 . Because I believe that it may be helpful to others too I share it here. Notice that in the constructing string each char can be present only once. Example of usage: $numCode = new Model_Utils_Numcode("4fFiV8kRTvm5MPNDcyO1dg7lr20Qtn3X6pKLZUqaEsxCwubGYIzhSWJojHeA9B"); $num = 3674; $encoded = $numCode->encode($num); echo "<br/> $num encoded = $encoded"; // 3674 encoded = Ac echo "<br/> $encoded decoded = ".$numCode->decode($encoded); // Ac decoded = 3674 Class: class Model_Utils_Numcode …

Member Avatar
Member Avatar
+2 forum 5

The following snippet is a simple PHP Pagination Script which I wrote. As far as I know it works fine. It is meant to be used within a class, and I have wrapped in in a class in order to demonstrate it's usage. The example given below will return something similar to the following: Pages: [B][ 1 ][/B] [URL="http://mysite.com/index.php?page=2"]>[/URL] [url=http://mysite.com/index.php?page=3]>>[/url] When there are a sufficient number of pages present, a Last Page link is added. When you navigate away from the first page, a previous page link is added. When you navigate past the second page, a first page link …

Member Avatar
Member Avatar
+2 forum 7

<?php $height = 100; for ($i=0; $i<$height; $i++){echo str_repeat(' ',($height-$i)).str_repeat('*',$i).'<br />';} ?> May you Daniweb people Grow Like this one.

Member Avatar
Member Avatar
+0 forum 1

When using emails as usernames you want them to be unique over your table, but this can be a problem if you consider a GMail account, because of their [username](https://support.google.com/mail/answer/12096?hl=en) [policy](https://support.google.com/mail/answer/10313?hl=en). They allow: * dots * digits * letters * plus addressing text by using the `+` sign, i.e. `orange+juice@gmail.com` * length between 6 and 30 characters, excluding dots and the appended part But when resolving the username they do not consider: * dots * different capitalization * plus addressing So, when you write to: UserName@gmail.com u.sername@gmail.com user.name+forum@gmail.com .u.s.e.r.n.a.m.e.@gmail.com u.serName+doh@googlemail.com You will always match the same account: username@gmail.com This class …

Member Avatar
Member Avatar
+2 forum 1

first create the table [CODE] create table images ( image_id serial, filename varchar(255) not null, mime_type varchar(255) not null, file_size int not null, file_data longblob not null, primary key (image_id), index (filename) ); [/CODE] the file to output the images to the browser [B]picsrc.php[/B] [CODE]<?PHP //detect if image is called correctly if($image){ //connect to db $db = mysql_connect('localhost', 'user', 'password'); //find the image $query = sprintf('select * from images where filename = %d', $image); $result = mysql_query($query, $db); $image = mysql_fetch_array($result); //tell the browser the url is an image header('Content-type: ' . $image['mime_type']); header('Content-length: ' . $image['file_size']); //finally output the …

Member Avatar
Member Avatar
+0 forum 4

Here is a simple way to insert into a database that isn't much harder than using string concatenation - which we all know is very dangerous due to SQL injection attacks. Put the code snippet into `database.php`. Now, in a script handling a form post, such as `post_reply.php`: <?php require_once("database.php"); // Get session and post data to insert - no need for mysql_real_escape_string or other escaping // (Note: Passwords should still go through password_hash() or crypt() though!) $member_id = $_SESSION['member_id']; $response_to = isset($_POST['response_to']) ? $_POST['response_to'] : (isset($_GET['response_to']) ? $_GET['response_to'] : ''); $subject = isset($_POST['subject']) ? $_POST['subject'] : ''; $post = …

Member Avatar
Member Avatar
+4 forum 6

Hi All, Been a bit bored, so went at it to create a class from some long procedural stuff I found littered in various guises all over the web. The code I used as a base for the class came from [here](http://www.codediesel.com/php/downloading-gmail-attachments-using-php/). As this uses IMAP functions and many of you out there use XAMPP on your local machines, ensure that you enable it in php.ini before testing this script. Search the file for `;extension=php_imap.dll` and take off the preceding semi-colon to leave `extension=php_imap.dll`. Save the file. You then need to restart Apache. Usage is pretty simple - just three …

Member Avatar
+4 forum 0

Hi All, thought I'd pass on a (very) simple password class which is built on the new `password_hash()` and `password_verify()` functions. You will need php 5.5.0+ in order to get this to work. If you're thinking of modifying this and using it in a production site, please check that your host has 5.5.0+, as many do not. To be honest there's not much going on beyond the two functions mentioned, but it can be modified easily to change any validation rules. [VERSION 1.1 with improved validation and rehashing](https://www.daniweb.com/web-development/php/threads/485519/a-simple-password-hash-and-verify-class#post2125105) (as suggested by veedeoo and ~s.o.s~) Comments, improvements etc. most welcome.

Member Avatar
Member Avatar
+8 forum 9
Code Snippet Form Helper Class

The purpose of this class is to be able to create an html form with ease. This is inspired by the form helper in CodeIgniter. This can easily adapt to bootstrap or any css and jquery where id is required. To use this class, we need to include the class provided below. require_once('FormHelper.php'); ## we create an instance of the class $form_create = new FormHelper(); we can also dynamically create <select>, by defining the option items as an array $select_array = array('php','java','html','javascript','python','perl','jquery'); generate the form echo $form_create->form_option('post', 'processor.php', 'myform', 'coolform'); echo $form_create->form_label('Name', 'name'); echo $form_create->form_input('text', 'name', '', 'input_class', 'type your …

Member Avatar
Member Avatar
+3 forum 1

FPDF is a class that provides a useful way to deal with PDF documents using dynamic content. Sometimes, according to a special circumstance, also would be valuable to send directly the PDF as attachment e.g. send an invoice automatically after processing a payment. In this example we use a html message and FPDF / mail() techniques.

Member Avatar
Member Avatar
+0 forum 25

Hello All. Been playing around with some linked dropdowns. It seems to be a recurring theme here on DW, so I thought I'd offer this up as a possible solution or for discussion, to see how it could be improved. The premise for this set of linked dropdowns is that it is not dependent on php or Ajax following page load, that is to say, all the data is initially dumped into json format from the database and then everything is left to javascript. I've used jQuery to produce a plugin, but the same functionality is possible with just plain …

Member Avatar
Member Avatar
+5 forum 3

Hello Everyone, I wrote this script way back in 2008 as Version1. However, I was forced to forget about this, because it was criticized by many developers with BS, MS, and "Seasoned PHP Developer" under their names. During those days, I couldn't stand up for my own reasons, because I can't even put High School graduate as my lone achievement under my name. I was told this script was 10 steps backward to where we at in 2008. If my memory still served me well, even my own brothers in Silicon Valley have asked me to forget about this. **enough …

Member Avatar
Member Avatar
+2 forum 7

**// EDIT 2014-04-07 //** New version (1.0.3) posted at the [bottom](http://www.daniweb.com/web-development/php/code/476623/ajaxed-linked-dropdowns-select-fields-for-volatile-data#post2082327) Demo Page: http://demos.diafol.org/ajax-linked-dropdowns.php *********************************************** Hello All. Been playing around with more linked dropdowns (select form fields), following the code snippet posted for static-ish data [here](http://www.daniweb.com/web-development/php/code/475238/linked-dropdowns-select-fields-for-static-ish-data). The static-ish version is of limited use (or of no use!) if data in the DB is updated regularly. So, here goes... No data is loaded by PHP initially, everything is provided through an Ajax call, however, that could be easily modified if required. I've used jQuery once again, as my plain javascript skills just aren't up to the job, and have produced a …

Member Avatar
Member Avatar
+3 forum 4

This is a function that I wrote and have re-used many times. I share it with you DaniWebers to use, laud or destroy! In a nutshell, this takes a user's search box input phrase and generates a series of LIKE clauses for your SELECT statement. Looks like "col1 LIKE '%s_term1%' OR col1 LIKE '%s_term2%' OR col2 LIKE '%s_term1' .." The column names you want it to use is the second parameter of the function.

Member Avatar
+1 forum 0

Raffle function to generate unique numbers for each player, related to the same table with a recursive aproach. I had to do a simple raffle system for a contest that I was working on and tried many things and the most simple and the most effective one to generate unique numbers for each player was this, a simple recursive function.

Member Avatar
Member Avatar
+0 forum 3

The End.