Jump to content

Junitar

Members
  • Posts

    35
  • Joined

  • Last visited

Everything posted by Junitar

  1. Junitar

    Constants usage

    Got it. Thank you very much!
  2. Junitar

    Constants usage

    Thanks for the reply. But then, what's the benefit of using, e.g. dirname(__DIR__) over $_SERVER['DOCUMENT_ROOT']?
  3. Junitar

    Constants usage

    Hello, I was wondering if it's good practice and if it's safe to define constants that use magic constants to access various directories in a project, even directories outside the webroot. I've found many documentations that discuss how useful are constants when debugging, but what's the usual usage apart from that? For example, is it good practice to write: <?php define('WEBROOT', __DIR__); define('ROOT', dirname(WEBROOT)); define('CORE', ROOT.'/core/'); define('CONFIG', ROOT.'/config/'); define('ADMIN', ROOT.'/admin/'); include CORE.'includes/header.inc.php';
  4. Oops, my bad… Messed up the variable name I wrote in the post with the real variable name I used in my code. It's of course !in_array($id, $galleriesId) Though, I did make a mistake with the || and &&, thanks for pointing it out! Ok, that was it. I had HTML above the PHP code. I've just made the changes to my code, placing the PHP part containing the header() before the doctype tag and it's now working. Spent so much time trying to figure out what was wrong… wasn't even aware of that. Well thank you very much for the help dsonesuk. Thanks also Gabrielphp for your suggestion.
  5. Hello, I've a photo gallery on my site that loads its content based on URL parameter (which is basically the name of the folder the images are taken from). I would like to make a redirection to the main galleries page if the parameter does not exist, is empty or wrong. I'm using an if statement with the header function but can't manage to get it work. $galleryId is the id of the current gallery and $galleriesId is an array containing all the actual galleries id. Here is the code: $id = $_GET['id']; if (isset($id) || !empty($id)) { // Check if the current gallery id matches an actual gallery id if (!in_array($galleryId, $galleriesId)) { header("Location: ../galleries.php"); exit; } else { // ... } } else { header("Location: ../galleries.php"); exit; } The correct URL has the following format: mysite.local/galleries/gallery.php?id=gallery-id If I change the id value to, e.g. wrong-id, nothing happens. I just have the new URL mysite.local/galleries/gallery.php?id=wrong-id with every bit of HTML code before the exit function being executed. Not sure if that makes a difference but I work on a virtual local host. How can I do to redirect to the main galleries page when I have an invalid parameter? Thanks
  6. Thanks for your reply. If I understand correctly, all I've to do to protect my gmail password and username once my site is ready to go online, is to organize my folders on the server that hosts my website like so: :SERVER: myMainFolder _________|_________ | | iniFilesFolder websiteFilesFolder | ______|___________________ | | | | file.ini index.php contact.php ….php and then retrieve my password by adding the following lines in my PHP script: $ini = parse_ini_file('/myMainFolder/iniFilesFolder/file.ini', true); $mail->Username = $ini['email']['username']; $mail->Password = $ini['email']['password']; With the file.ini being something like: [email] username = myUsername password = myPassword Is that correct?
  7. You have to set your html and body height to 100% to make your div height: 100% work. I guess you want to center your logo vertically like in the image you showed. To do so, you can either use flexbox or if you want to support IE ≤ 9, you can wrap your logo in a div the size of your logo and style it like so .logo_wrapper { position: absolute; top: 0; right: 0; bottom: 0; left: 0; height: 100px; /* height of your logo */ width: 100px; /* width of your logo */ margin: auto } BTW, you've an extra closing div in your code right after the closing script tag.
  8. Hi, I'm using PHPMailer to send email from a contact form using gmail SMTP which requires to include the SMTP password directly in the PHP script like so $mail->Password = 'mypassword'; According to what I've found on the net, it seems that it's not recommended since the password can be easily hacked. Thus, I'm wondering how to do to protect my password. I've found people recommending to put the password in an INI file outside the webroot and then to retrieve it using parse_ini_file() function. My problem is that I'm not sure I understand the "outside the webroot" part… if anyone could explain this to me and how to do it, it would be much appreciated. Also, should I protect the INI file with a .htaccess? Thanks.
  9. Thank you very much for the example!
  10. Thank you very much for your input and for the link. I will test it as soon as my site is online. 1a and 2b (after switching <section> with <article> and vice versa) would then be the best options. What do you think about wrapping the orange and red elements into a <section> like in 1a? Knowing that there won't be any other <section> in the <main> tag, do you think it still makes sense or it's just unnecessary? I would tend to say it's unnecessary, can you confirm that?
  11. Hello, I’m restructuring the html code of my site to have the best semantic markup as possible but I struggle with the <article> and <section> tags. I know there are lots of documentations on the internet on how to use these tags. I’ve read many of them but the more I read about <article> and <section>, the more I get confused on how to use them properly, mainly because I’ve seen a lot of contradictory examples from a site to another. I understand that a <section> is used to regroup elements sharing information on a similar thematic whereas an <article> refers to an independent content that is reusable outside the document. It sounds simple in theory but in practice, it's very tricky, at least for me. I’ve made different templates of my "about" page which is he most complex page I have (in regard to semantic), and I would appreciate if you could tell me which of the following images is the most semantically correct or if you have other suggestions I could take into consideration. Beside the <section>/<article> thing, I also question myself on the need of using a single <section> in the <main> container… Do I need to regroup all my "about" elements inside a <section> tag if there is no other <section> in the document? Fig. a Fig. b and 4b -> just like 3b with the blue containers being different <section> instead of being different <article>. 4b is how my page is currently structured. Any help would be appreciated. Thanks!
  12. I'm not a PHP expert but for your first question, I would recommend you to use the FilesystemIterator class (implemented in PHP ≥ 5.3). I've just finished doing an image gallery for my project and this is the easiest, most straigth-forward approach I've found. Here is an example on how to use it: $dir = '/yourDirectory'; $fileDir = new FilesystemIterator($dir); // creates a new instance of the class as $fileDir foreach ($fileDir as $file) { if ($file->getExtension() == 'jpg') { // lists all .jpg in $dir echo '<img src="'.$file.'"/> '; } } Hope that helps.
  13. Works like a charm. Thank you very much!
  14. Hi, I would like to list all the items contained in a folder except for 2 or 3 specific ones. For example, let's say I've item 1, item 2, …, item 10 in /myFolder. I would like to list all the items of /myFolder but item 1, item 3 and item 4. I've come up with the following code which does the work but looks a bit awkward. <?php $dir = '/myFolder'; $filesDir = new FilesystemIterator($dir, FilesystemIterator::SKIP_DOTS); $fileName = ''; foreach ($filesDir as $file) { $fileName = $file->getBasename(); if ($fileName != 'item1' && $fileName != 'item3' && $fileName != 'item4') { echo $fileName.'<br>'; } } Is there a simple and more elegant way to accomplish this? Any help would be appreciated, thanks!
  15. Thanks for the clarification! Everything's clear.
  16. Thank you very much Ingolme for your example, I understand it now! Yes, I guess it's a good enough reason. Sorry if my question was dumb but I wanted to understand why it was a bad thing not to initialize variables first. I started learning PHP 2 days ago and I don't have enough experience in programming to know it could cause a program to crash. I got it now anyway, thanks.
  17. Thank you to you both for your thorough feedbacks. I'm afraid it's still a bit fuzzy in my mind right now and the fact my English is limited sure doesn't help. I understand that a variable is used to store a content and it doesn't make sense to call a variable that doesn't exist. But in my example, I declared my variables later in the code: … $errors['name'] = 'alert'; … $name = sanitize_input($_POST['name']); … $email = sanitize_input($_POST['email']); … this is why I don't completely understand the need of specifying at the beginning that $errors = [], $name = '', $email = '', etc. If I write: <?php $test['letter1'] = 'a'; $test['letter2'] = 'b'; print_r($test); // the output is Array ( [letter1] => a [letter2] => b ) my array is well declared and ready to be used without being initialized using $test = []. I guess the answer to my question is somewhere in and
  18. Hello, I've just read the tutorial on how to process a form in PHP, available on this site, and I was wondering if there's a specific reason to declare empty variables (or arrays) at the beginning? I don't understand what is the point of doing so. For example, in the following code, why would the 2sd and 3rd lines be necessary? <?php $errors = []; $name = $email = $message = ''; if (condition) { $errors['name'] = 'alert'; } else { $name = sanitize_input($_POST['name']); if (new_condition, $name) { $errors['name'] = 'new alert'; } }
  19. Thank you to confirm my thoughts. I had a doubt after I stumbled upon an old thread on stackoverflow where a guy implied this kind of code was possibly flawed.
  20. Hi, I'm new to PHP and I was wondering if it's perfectly safe to use $_SERVER['PHP_SELF'] like so: <body<?php if(basename($_SERVER['PHP_SELF']) == 'home.php') echo ' class="home"'; ?>> … </body> As far as I understand, the $_SERVER['PHP_SELF'] variable can only be exploited when used as a link or in a form/inputs, where the variable should be wrapped into htmlspecialchars() to counter XSS attacks, am I right?
  21. Hi, I'm making a responsive centered tiled gallery for my website. For aesthetic reasons, I only need to have a margin between the images. To do so, I set a left margin to each elements but the first, in each row, using the :nth-child() selector. I got it to work as I wanted to but I'm now facing a problem with the image captions which break the floating elements if I enter too many words in them. Please, see the jsfiddle example here: https://jsfiddle.net/0w379712/ How can I do to prevent this? Ideally, I would like to have the entire row going down if there's too much text above. I thought a clear: left in the last-child of the row would fix this but it's not working. Also, would you recommend to use the :nth-child() or :nth-of-type() selector to achieve this? I've analysed several web galleries with similar mosaic and none of them used these kind of selectors. Probably for browser compatibility reasons, but can't say for sure. Edit: I finally managed to fix the problem using .tile:nth-child(2n+1) / .tile:nth-child(3n+1) { clear: left }, I messed up with the syntax of the selector in my previous attempts…
  22. I've just tested again your suggestion and it works indeed. Don't know exactly what I previously did. I guess my brain just needs some rest right now, I've done too many exercices today, it's a big mess in my mind
  23. Thanks for the help, I've already tried btn.attr("title", currentText == "menu" ? "close menu" : "menu"); but it didn't work properly (the title changed to "close menu" when it should be "menu" and vice versa). Anyway, I've just found what I was doing wrong… btn.attr("title", btn.attr("title") == "menu" ? "close" : "menu"); is now working fine but I formerly called my button title "Menu" with a capitalized "m", while I used a small "m" in the btn.attr() which screwed it all.
  24. Hi, I'm training myself at jQuery doing some exercices. I've written the following code: <button id="btn" title="menu">menu</button> jQuery(function($){ $("#btn").click(function(){ var btn = $(this); var currentText = btn.text(); // alert(currentText); if (currentText == "menu") { btn.text("close"); btn.attr("title","close menu"); } else { btn.text("menu"); btn.attr("title","menu"); } }); }); and I would like to simplify it as much as possible, using if else statement shorthand. I've managed to get it works for the .text() method btn.text(currentText == "menu" ? "close" : "menu"); but not for the .attr(). I've tried btn.attr("title", "menu" == "menu" ? "close" : "menu"); and btn.attr("title", btn.attr("title") == "menu" ? "close" : "menu"); among many other things but I can't figure out how to make it works. Any help?
  25. Thank you to you both for your input, I really appreciate your help. I will follow your advices and don't bother with old versions of IE then.
×
×
  • Create New...