178 Reusable Code Snippet Topics

Remove Filter
Member Avatar for
Member Avatar for olaf

This function is usefull for peoples with a link directory where the links are stored inside a (MySQL) database. The PHP script is easy to use to find out if a reciprocal link still exists on the corresponding webpage. It takes care about the trailing slash in urls and can …

Member Avatar for Dani
0
179
Member Avatar for Dani

Sometimes we have a need to take a simple PHP array and represent it as an encoded string. One use case that we have for this at DaniWeb is when a new user signs up, and we ask them to click on a link that was sent to them via …

Member Avatar for Dani
2
205
Member Avatar for Dani

Sometimes we want to know if the webpage was fetched over an SSL connection (e.g. the URL begins with https:// instead of http://). This way, if an end-user is accessing an insecure version of our site, we can redirect them to the secure version. The following PHP function called `no_ssl()` …

2
191
Member Avatar for Dani

For some reason, PHP class constants don't play nicely with inheritance. For example, suppose you have the following code: class Foo { const VAR = 'foo'; public function __construct() { echo self::VAR; } } class Bar extends Foo { const VAR = 'bar'; } // This will actually print out …

Member Avatar for Dani
1
287
Member Avatar for Dani

By default, PHP's clone keyword just creates a shallow copy of an object, and doesn't work as desired if the object contains properties that are also objects, because it doesn't create real copies of those objects but rather just pointers to the initial version of them. This function creates a …

0
251
Member Avatar for Dani

A little while ago, I wrote a [tutorial](https://www.daniweb.com/programming/web-development/tutorials/537376/sanitize-php-user-input-strings) about how important it is to sanitize PHP user input strings. Not only is it important to sanitize user input being fed into a database query, but it's also important to sanitize user input being displayed to the end-user to generate valid …

2
195
Member Avatar for Dani

DaniWeb is built on top of the Codeigniter 3.1.x PHP framework. Although I probably should have built it as a CI model, here is the database library that we are using. You can see it mainly serves as a wrapper for CodeIgniter's built-in database class. You can see we use …

1
129
Member Avatar for martin5211

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 …

Member Avatar for Dani
0
22K
Member Avatar for joshl_1995

Hello Daniweb Community, I have been talking on a couple [Facebook](http://www.facebook.com) groups such as sale pages where you post your own stuff for people to buy. Most groups like this have rules about bumping like once per day, every 12 hours etc... So it got to the point where it …

Member Avatar for Mark_109
4
3K
Member Avatar for Tko_1

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 for rproffitt
0
4K
Member Avatar for NardCake

Hello! I needed a configuration file for a application I'm working on so I first used the built in ini functions in php but it wasn't easy to write the ini files. So I wrote myself a class here to parse a configuration file and write/edit configuration files. I plan …

Member Avatar for Vitaly_1
2
951
Member Avatar for Dani

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 for bangalore.webguru
0
3K
Member Avatar for Dani

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

1
3K
Member Avatar for Dani

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.

1
2K
Member Avatar for Dani

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 …

0
2K
Member Avatar for Dani

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 …

0
2K
Member Avatar for Dani
0
2K
Member Avatar for Dani

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

Member Avatar for PARDEEP_2
1
1K
Member Avatar for cereal

###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 …

0
270
Member Avatar for jkon

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 …

Member Avatar for jkon
2
655
Member Avatar for shubhamjain1

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

Member Avatar for Tùng
2
8K
Member Avatar for tellysk
Member Avatar for cwarn23

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 …

Member Avatar for rubberman
1
997
Member Avatar for diafol

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 …

Member Avatar for diafol
4
12K
Member Avatar for digital-ether

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 …

Member Avatar for lps
0
4K
Member Avatar for jkon

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 …

Member Avatar for diafol
2
3K
Member Avatar for DennisP

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 …

Member Avatar for Wolf_2
2
3K
Member Avatar for imti321

<?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 for imti321
0
271
Member Avatar for cereal

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` …

Member Avatar for cereal
2
651
Member Avatar for metalix

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 …

Member Avatar for guruparthi
0
1K

The End.