1 2 3 4 5 |
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; // echo $actual_link; |
Category: PHP
Free PHP, HTML, CSS, JavaScript/TypeScript editor – CodeLobster IDE
In this article, we suggest you to get acquainted with the free editor of web languages – CodeLobster IDE. It is presented on the software market for a long time already, and it wins a lot of fans.
CodeLobster IDE allows you to edit PHP, HTML, CSS, JavaScript and TypeScript files, it highlights the syntax and gives hints for tags, functions and their parameters. This editor easily deals with those files that contain a mixed content.
If you insert PHP code in your HTML template, then the editor correctly highlights both HTML tags and PHP functions. The same applies to CSS and JavaScript/TypeScript code, which is contained in HTML files.
The program includes auto-completion function, which greatly speeds up the programmer’s work and eliminates the possibility of errors.

CodeLobster IDE provides contextual help on all supported programming languages, it uses the most up to date documentation at this moment, downloading it from official sites. So we can quickly get a description of any HTML tag, CSS attribute, PHP or JavaScript/TypeScript function by pressing the F1 key.
The built-in PHP debugger allows you to execute PHP scripts step by step, sequentially moving through the lines of code. You can assign check points, view the process of the work of loops, and monitor the values of all variables during the execution of the script.
Other useful functions and features of the IDE:
- A pair highlighting of parentheses and tags – you will never have to count parentheses or quotation marks, the editor will take care of it.
- Highlighting of blocks, selection and collapsing of code snippets, bookmarks to facilitate navigation on the edited file, recognition and building of the complete structure of PHP projects – these functions ensure easy work with projects of any scale.
- Support for 17 user interface languages, among them English, German, Russian, Spanish, French and others.
- The program works on the following operation systems: Windows 7, Windows 8, Windows 10, Mac OS, Linux, Ubuntu, Fedora, Debian.
The professional version of CodeLobster IDE provides the programmer with even more features.
For example, you have an opportunity to work with projects on a remote server with use of the built-in FTP client. You can edit the selected files, preview the results and then synchronize the changes with the files on the hosting.
In addition the professional version includes an extensive set of plug-ins:
- Fully implemented support for JavaScript (and TypeScript) libraries, such as jQuery, Node.js, AngularJS, AngularTS, BackboneJS, EmberJS, VueJS and MeteorJS.
- A large set of extensions that help to work with PHP frameworks – CakePHP, CodeIgniter, Laravel, Phalcon, Smarty, Symfony, Twig and Yii plug-ins.
- Plugins for working with the most popular CMS – Drupal, Joomla, Magento and WordPress.
- Also CodeLobster IDE has special plug-in for Bootstrap.
We can download and install any framework directly from the program without being distracted from the main tasks.
In general, for a year of work, our team had no complaints against the editor. CodeLobster IDE works fast, does not hang and allows us to work even with large PHP projects.
You can download CodeLobster IDE from the original website http://www.codelobster.com/.
PHP – make timestamp from normal date/time string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php date_default_timezone_set("Europe/Bucharest"); echo time(); echo "<br><br>"; $d=mktime(10, 01, 00, 8, 12, 2019); echo $d; echo "<br><br>"; $data1 = "18/05/2019"; $x = (explode("/",$data1)); echo $x[0]; echo $x[1]; echo $x[2]; echo "<br><br>"; $ora1 = "11:00"; $y = (explode(":",$ora1)); $m_y = $y[0]-2; echo $m_y; echo $y[1]; echo "<br><Br><br>"; $d=mktime($m_y, $y[1], 00, $x[1], $x[0], $x[2]); echo $d; ?> |
Download(Forced) PDF file with PHP HEADER
1 2 3 4 5 6 7 8 9 |
<?php header("Content-type:application/pdf"); // It will be called downloaded.pdf header("Content-Disposition:attachment;filename='file.pdf'"); // The PDF source is in original.pdf readfile("file.pdf"); ?> |
Redirect current domain to a different domain with PHP
1 2 |
header("Location: http://www.abc.com"); |
Send data to PHP via AJAX and get response
This is the simple script that I use every time I build one page sites… I use this code to update, delete, modify, add data to Mysql or other DB’s…
This code also get the class or id of the clicked link and can display a result there or in another div… this script can also be modified to play other important parts in the web app…
Hope it helps.
1 2 3 4 5 6 7 8 9 10 11 12 |
$('.get-delete').click(function() { var get_id = $(this).attr('id'); $("#" + get_id ).toggle(400); $.ajax({ type: 'POST', url: 'delete.php', data: { idx: get_id}, success: function(response) { $('#result').html(response); } }); }); |
PHP – set hour to Eastern European Time(Bucharest)
This is the test script, you normally just need the first line of code.
Enjoy!
1 2 3 4 5 6 |
<?php date_default_timezone_set("Europe/Bucharest"); echo date_default_timezone_get(); echo "<br>"; echo date("Y,m,d / G:i:s"); ?> |
jQuery (Ajax) Contact form with all elements
This is my default CONTACT form built with jQuery and CSS.
The form uses a jQuery call to a PHP script that inserts data into a Mysql DB and sends two emails of confirmation, all this under the hood… meanwhile, there is a CSS only animation that displays a message that the data is been sent to the server and shows a positive message if that data was sent without errors and an error message if there was an error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
$(document).ready(function(){ var request; $("#contact-form").submit(function(event){ event.preventDefault(); if (request) { request.abort(); } var $form = $(this); var $inputs = $form.find("input, textarea"); var serializedData = $form.serialize(); $inputs.prop("disabled", true); request = $.ajax({ url: "contact-form.php", type: "post", data: serializedData }); request.done(function (response, textStatus, jqXHR){ //console.log("Hooray, it worked!"); $(".show-success").slideToggle(); //$(".hide-contact").slideToggle(); }); request.fail(function (jqXHR, textStatus, errorThrown){ console.error( "The following error occurred: "+ textStatus, errorThrown ); $(".show-error").slideToggle(); }); request.always(function () { $inputs.prop("disabled", false); }); }); }); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
<style> .frm{padding:9px;border-radius: 3px;background:#ECEFF1;font-size: .8em; border:0px; width: 100%;font-weight: 300;box-sizing: border-box;} .frb{background:#3498DB} .pt-12{padding-top:12px}.small{font-size: .8em} .hide{display:none} .org-bg{background:#E74C3C;padding: 12px;border-radius: 3px;margin-bottom: 12px;color: #fff}.grn-bg{background:#52BE80;padding: 12px;border-radius: 3px;margin-bottom: 12px;} .orange-bg{font-family:'Quicksand',sans-serif;font-weight:400;font-size:.8em;} .fc{padding-bottom:12px;font-family:'Quicksand',sans-serif;font-weight:400;font-size:1.2em} #loading{display:none} .spinner { margin: 0px auto 0; width: 70px; text-align: center; } .spinner > div { width: 18px; height: 18px; background-color: #333; border-radius: 100%; display: inline-block; -webkit-animation: sk-bouncedelay 1.4s infinite ease-in-out both; animation: sk-bouncedelay 1.4s infinite ease-in-out both; } .spinner .bounce1 { -webkit-animation-delay: -0.32s; animation-delay: -0.32s; } .spinner .bounce2 { -webkit-animation-delay: -0.16s; animation-delay: -0.16s; } @-webkit-keyframes sk-bouncedelay { 0%, 80%, 100% { -webkit-transform: scale(0) } 40% { -webkit-transform: scale(1.0) } } @keyframes sk-bouncedelay { 0%, 80%, 100% { -webkit-transform: scale(0); transform: scale(0); } 40% { -webkit-transform: scale(1.0); transform: scale(1.0); } } .trimit{background: rgba(149, 192, 234);padding: 12px; border-radius: 3px;margin-top:12px;font-family:'Quicksand',sans-serif;font-weight:400;font-size:.8em;} </style> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<!-- form ROW --> <div class="row pt-12x"> <div id="loading"> <div class="spinner"> <div class="bounce1"></div> <div class="bounce2"></div> <div class="bounce3"></div> </div> <div class="trimit"><i class="far fa-paper-plane fa-fw"></i> Se trimit datele...</div> </div> <!-- Error Handlers --><div id="show-success"></div> <div class="grn-bg orange-bg list-font-1 hide show-success"><i class="fas fa-check-square fa-fw"></i> <b>Vă mulțumim!</b> Am primit mesajul dumneavoastră.</div> <div class="org-bg orange-bg list-font-1 hide show-error"><i class="far fa-question-circle fa-fw"></i> <b>Eroare!</b> Mesajul nu a fost trimis.</div> <!-- .Error Handlers --> <!-- form --> <form id="contact-form"> <div class="hide-contact"> <div class="fc"><i class="fas fa-user fa-fw brown"></i> Formular contact</div> <div> <input type="text" name="name" class="frm" placeholder="Nume" required=""> </div> <div class="pt-12"> <input type="text" name="email" class="frm" placeholder="Email@exemplu.com" required=""> </div> <div class="pt-12"> <input type="text" name="phone" class="frm" placeholder="Număr telefon" required=""> </div> <div class="pt-12"> <textarea name="message" class="frm" rows="6" placeholder="Mesaj..."></textarea><input type="text" name="city" value="" class="hide"> </div> <div class="pt-12 small"><i class="fas fa-lock fa-fw green"></i> Protecția datelor <small>(GDPR) (EU) 2016/679 <a href="https://en.wikipedia.org/wiki/General_Data_Protection_Regulation"><i class="fas fa-external-link-alt fa-fw"></i></a></small></div> <div class="pt-12"> <button type="submit" class="frm frb dsblb"><i class="fas fa-paper-plane fa-fw"></i> Trimite</button> </div></form> </form> </div> <!-- ./hide-contact --> </div> <!-- ./form-ROW --> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
<?php // v1.0 090318 if(isset($_POST['city']) && !empty($_POST['city'])) { echo "Hello Robot!"; exit; } if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) { echo "Hello Robot 2!"; exit; } $today = date("d/m/Y - G:i "); ///////////////////// /////////// ///////// /////// ///// // include_once("_connect.php"); // _POST Vars... $name = htmlentities($_POST['name']); $email = htmlentities($_POST['email']); $phone = htmlentities($_POST['phone']); $message1 = htmlentities($_POST['message']); $data = time(); $sql = "INSERT INTO table_name (name, email, phone, message, data) VALUES ('$name', '$email', '$phone', '$message1', '$data')"; if ($conn->query($sql) === TRUE) { ////// ////// /// // yeyy! Mail to owner... $to_owner = "me@example.com"; $subject_owner = "New message from..."; $message_owner = " <html> <head> <title>New message</title> </head> <body> <p><h3>Mesaj nou pe site...</h3></p> <p>Detalii mesaj:</p> <ul> <li>Name: $name</li> <li>Phone: <a href=\"tel:$phone\">$phone</a></li> <li>Email: $email</li> <li>Data: $today</li> <li>Message: $message1</li> </ul> </body> </html> "; // Always set content-type when sending HTML email $headers_owner = "MIME-Version: 1.0" . "\r\n"; $headers_owner .= "Content-type:text/html;charset=UTF-8" . "\r\n"; // More headers $headers_owner .= 'From: Name<contact@example>' . "\r\n" . "Reply-to: $email" . "\r\n"; $headers_owner .= 'Cc: carbon-copy@example.com' . "\r\n"; mail($to_owner, $subject_owner, $message_owner, $headers_owner); ///////////////// /// +++++++++++++++++++++ ///// exit; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?> |
Mysql: Table structure for table `contact`
This is the basic table structure for my contact section on many projects I work on…
The id field is auto_increment and the time field is just a PHP time() string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
CREATE TABLE `contact` ( `id` int(11) NOT NULL, `Name` varchar(30) NOT NULL, `Email` varchar(90) DEFAULT NULL, `Message` varchar(5000) DEFAULT NULL, `time` varchar(15) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; ALTER TABLE `contact` ADD KEY `id` (`id`); ALTER TABLE `contact` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; COMMIT; |
PHP & HTML – easy spam prevention using hidden form fields (sample code)
The easy and fast working method of securing your forms agains spam bots is as easy as a few lines of PHP and HTML code.
Spam bots, at least, most of them, crawl random websites and fill their forms with all kind of ads or unwanted content. There are a lot of alternatives to stop this, Google Captcha, reCaptcha, etc., I used all of this methods, they work pretty well but, sometimes, when you move your site to a new server or domain, you need to spend a lot of time working around this methods to make them work again.
I simply tried a really simple method that I found a few years ago, the method implies using simple PHP and HTML to trick the bot to fill a field that the user doesn’t see(hidden via CSS).
The code is pretty simple…
Here is the HTML…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<form action="sample.php"> <input type="text" name="name" placeholder="Nume" required=""> <input type="text" name="email" placeholder="Email" required=""> <input type="text" name="phone" placeholder="Telefon" required=""> <input type="text" name="city" class="vho" id="send" placeholder="Paris"> <button type="submit">Submit</button> </form> <style> .vho { display: none } </style> |
Here is the PHP…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php if(isset($_POST['city']) && !empty($_POST['city'])) { echo "Hello SPAM Robot!"; exit; } if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['phone'])) { echo "Hello SPAM Robot 2!"; exit; } ?> |