Jump to content

TechnoDiver

Members
  • Posts

    203
  • Joined

  • Last visited

Everything posted by TechnoDiver

  1. $num = 27; $first_digit = substr($num, 0); $last_digit = substr($num, -1); echo $first_digit; //this returns 27 echo $last_digit; //this returns 7 In the above example why is the first echo statement returning the full number whereas the second returns the last digit? (I know why the last digit is returned in the second the real question is why wouldn't the first return 2?)
  2. Yea, that works better, thanks for your time, requinix
  3. I ran flatpak list from the CLI - Yes, it says that VSCodium is installed through it EDIT: apologies for how stupid my last comment sounded, I had just opened my eyes from napping, I should have waited until I could have put in more effort to the response. My negligence, my error, apologies
  4. I'm going to be honest with you, requinix, I don't remember. I'm sure it was from the linux mint Software Manager and then I updated after that. Is there a way I can check that?
  5. I hope this is the right place for this post, if not I apologize but it's not immediately obvious given my question. I 'm trying to set up VS Codium to be able to run PHP interactively. I've installed the Code Runner extension and applied the following JSON settings: { "editor.minimap.enabled": true, "window.menuBarVisibility": "toggle", "window.zoomLevel": 0, "window.titleBarStyle": "native", "workbench.colorTheme": "Default Dark+", "php.validate.executablePath": "/usr/bin/php" } When I go to test it I get the following error: [Running] php "/opt/lampp/htdocs/cms/test.php" /bin/sh: line 1: php: command not found [Done] exited with code=127 in 0.047 seconds It's just a simple echo so I didn't make any typos and I'm assuming it's a problem with the $PATH even though a 'which' query on the CLI gives the path I used above. I'm really reluctant to start messing around with $PATH related things without knowing more. Can someone give me some guidance on how to sort out this issue please?! TIA
  6. Thank you for your response, I didn't notice it until now. I'm still decyphering it Barand, I had to work on something else for a few days so I'm just getting back to this. I saw the mistake with assigning an empty array and the checking if it were empty immediately afterwards. That was certainly a rookie mistake. But there's still something not clicking. 1. I checked for errors in the assign_field() function (in this case $password) 2. I called the check_errors function with the right parameters. 3. The function check_errors() checks for an empty array, merges them with the others if they exist or returns the $field if everything is ok. I'm really baffled. I'm not sure what else you were suggesting I need to do. Here's the code as it stands now: // PASSWORD (MANDATORY) //validate, confirm assign password function assign_password() { $password = clean_password($_POST['password']); $password2 = clean_password($_POST['password2']); $errors = []; if($password == $password2) { //password 6-30 characters long if (strlen($password) < 6 || strlen($password) > 30) { $errors[] = "Your password must be 6-30 characters long"; } // FOR DISPLAY PURPOSES, THIS WILL UNDERGO VAST CHANGES EVENTUALLY //password is alpha-numeric if (!ctype_alnum($password)) { $errors[] = "Your password must be English characters and/or numbers"; } } else { $errors[] = "Your passwords don't match"; echo "Your passwords don't match"; } check_errors($errors, $password); } // END PASSWORD and check_errors: //this function appears in username, email and password_assign() //it simply checks for any content in the error array and assigns the field if no errors function check_errors(&$all_errors, $field) { if(!empty($errors)) { $all_errors = array_merge($errors, $all_errors); return false; } else { return $field; } } I've gone over it in my head dozens of times, tried mentally tracing exactly what the data was doing on each step, put it away for awhile to study other areas of PHP hoping for an AH HA! moment, read through your 'pseudo-code' in your last comment, went to Edabit to practice with functions. I still can't see what's wrong here, and I'm sure it's something basic. Is it a failure in my logic? My implementation?
  7. Ok, thanks. I'm obviously not using this particular code for anything, just practicing so I'll move forward. Out of curiosity, if I wanted to deactivate this type of 'error return' in my IDE (VSCodium) what would I even look for? Is there a specific term for this type of occurrence?
  8. Yea, it works for me too. I use VScodium and I'm getting the red lines under all those constants saying they're undefined, but I just tried to run it anyways and I get the "We are connected" echo. So I've learned that the IDE isn't always correct in it's error reporting. Is this a known issue? Something I can correct? The file tab is red (error red) the folder that contains the file in explorer is error red. Why is this coming back as error but still seems to be functioning?
  9. I'm messing around with unconventional coding practices, just to .... practice. The following code isn't being used for any amazing projects or anything, it's just me messing around with things getting practice. It's a simple code but it keeps telling me the constants are undefined. //defining the variables through an array $db['db_host'] = "localhost"; $db['db_user'] = "root"; $db['db_pass'] = ""; $db['db_name'] = "cms"; //fancy way of making them constants foreach($db as $key => $value) { define(strtoupper($key), $value); } $conn = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); if($conn) { echo "We are connected"; } Simple, simple, very simple but can someone tell me why it's not creating the constants? All of them in $conn are coming back as undefined.
  10. so I didn't want to start a new thread for this and I couldn't append it to my last post. I'm not trying to be too clever with the program but I decided to try something to help me understand more how data moves between functions. I took the code you've been helping me with and made a function from it -> function check_errors(&$all_errors, $field) { $errors = []; if($errors) { $all_errors = array_merge($errors, $all_errors); return false; } else { return $field; } } it's an internal function to be used in the assign_username, email and password functions. check_errors($errors, $password); //or check_errors($errors, $username) etc And this is another example of just when I started feeling comfortable with something I prove myself wrong. I'm finding it really frustrating because I feel that I can picture the data moving through the functions but why wouldn't the above work? I feel there's a simple concept that I'm missing or overlooking. I hate to be the guy who keeps asking about the same thing over and over, but I really want to understand how the data moves in a web app and I feel a good way is to let people respond however than process it all later into something cohesive. That one person may just leave a response that suddenly flicks the light bulb on Saying all that. What is wrong with this function, please? I'm really not grasping why it's not returning the error string (it's the empty array again).
  11. That just shot to shit everything I thought I understood about references in this instance. It also taught me to try everything and not assume something won't work because I can't understand how it does. I tried $errors = array_merge(.......); and didn't try $all_errors I just didn't see how it could work that way. Thank you, you've been a great help, but tell me if you don't mind - why $all_errors here? I can't contextualize it working here at all
  12. I had a good chuckle at this. From MY perspective it doesn't seem simpler. I've copied it to analyze it later and I've compared it to your previous example. I've looked over my amended code, based on your first example, and I completely understand it now, thanks for the new example. The reason to reference the variable is more clear now too. I'm working through a tutorial that teaches PHP through a CMS project. I spend some time on the tut then try to transfer that knowledge to a personal project I'm working on, the one this is the eventual registration form for. I've edited my code based on the concepts you introduced me to in your first example and have this: in functions.php - // USERNAME (MANDATORY) //uses the clean_names() and checks for empty input function assign_username(&$all_errors) { global $conn; $username = $_POST['username']; $errors = []; if($username) { $username = clean_names($username); //i've tried this in the negative too "if(!$username)...." } else { $errors[] = "A username is mandatory"; echo "A username is mandatory <br>"; } //check for duplicate username and return $username if doesn't exist already $username_check = mysqli_query($conn, "SELECT username FROM users WHERE username='$username'"); $number_rows = mysqli_num_rows($username_check); if($number_rows > 0) { $errors[] = "Username already in use <br>"; echo "Username already in use <br>"; } if($errors) { array_merge($errors, $all_errors); //i've tried this with $errors = array_merge(....) and still didn't work return false; //if I use echo ".." it echos but shows an empty array } else { return $username; } } // END USERNAME and //display error array function show_errors($errors) { print_r($errors); } and registration_handler.php looks like this: if(isset($_POST['register_button'])) { $errors = array(); //assign clean form variables $firstname = assign_firstname(); $lastname = assign_lastname(); $username = assign_username($errors); $email = assign_email(); $password = assign_password(); $date = date("Y-m-d"); show_errors($errors); echo "<br>"; echo "First Name: " . $firstname . "<br>"; echo "Last Name: " . $lastname . "<br>"; echo "Username: " . $username . "<br>"; } Once I start feeling more comfortable with PHP I run into a problem like this. I obviously still need some work transferring data between functions and other files, I thought I understood it, this code looks functional to me but yet it still shows Array() as the output without content. I've tried an 'echo' statement everywhere something is supposed to happen and always get the echo, so that part of the code is running, but "Username is Mandatory" is not being inserted into the $errors[] array. What more is there? I make a mental checklist of how I've come to understand how the data moves -> assigned array in both the file and the function, set the function parameter, added the string to the array, sent the array to a print function and called the print function in the other file. I can't fathom what more could possibly need to be done, obviously there's something, but I'm not seeing it. What have I missed?
  13. Barand, I've been reading about referencing variables and I understand it in principle but maybe not entirely in practice. Not sure I understand the use of it here. I have a few questions if you don't mind - 1) errors[] needs to be assigned within every function that uses it AND in the registration_handler.php as well as be referenced in the parameters? I understand the last 2, but didn't think it needed assigning in each function too 2) What is internally happening with a program when it runs the return false; in the if statement? 3) Would you mind giving me a ELI5 breakdown of why referencing the variable is done here and how that works throughout the rest of the program, only if you have the time and will, no pressure there. I'm getting the same result with your code as I was with how I was doing it (just getting Array() as output with no contents) and I thought that once I got more comfortable with the above mentioned issues I'd be able to figure it out. Thanks again
  14. I'm making a registration form and I'm really new at it. I've had a look about at similar issues but feel like I've got my code set up right, I obviously don't. I have a registration_handler.php and a functions.php file. I'm simply trying to pass an array ($errors[]) from the functions that generate them to a function that prints them out on registration_handler.php. It seems so easy, but I'm not getting it. Here's my code: from functions.php: (this is just an example of one of the functions) // USERNAME (MANDATORY) //uses the clean_names() and checks for empty input function assign_username() { global $conn; $username = $_POST['username']; if(!$username){ $errors[] = "A username is mandatory"; echo "A username is mandatory <br>"; } else { $username = clean_names($username); } //check for duplicate username and return $username if doesn't exist already $username_check = mysqli_query($conn, "SELECT username FROM users WHERE username='$username'"); $number_rows = mysqli_num_rows($username_check); if($number_rows > 0) { $errors[] = "Username already in use <br>"; echo "Username already in use <br>"; } else { return $username; } return $errors; } // END USERNAME Also from functions.php, the function to print out the error array: //display error array function show_errors($errors) { print_r($errors); } and the function call from registration_handler.php: if(isset($_POST['register_button'])) { //assign clean form variables $firstname = assign_firstname(); $lastname = assign_lastname(); $username = assign_username(); $email = assign_email(); $password = assign_password(); $date = date("Y-m-d"); $errors = array(); show_errors($errors); } I'm intentionally leaving the username out to test this on the form but I only get returned Array() It echos "Username is mandatory" but it seems like it's not passing the error string to the error array Can anyone point me towards what I'm doing wrong, please. I appreciate the replies
  15. Yea, that works great, maxxd, thank you. The only change I had to make was inserting a forward slash in front of ../config/config.php to make it '/../config.config.php'. In case another user is reading this, it might save them some aggro
  16. Ok, I get that. The doc root is /opt/lampp/htdocs. The path to config.php is /opt/lampp/htdocs/site/config/config.php So do you mean that it's best to move /config/config.php from the /site/ directory to the /htdocs/ directory? Or even outside /htdocs/ to somewhere else?
  17. It wouldn't let me edit my last comment but I realized this absolute path won't work when the site goes live. I'll have to go through and change the paths to config.php when that time comes. It's not outside of doc root. What path format should be used in this situation so the paths won't need to be changed later?
  18. Thanks, I fixed it by adding the entire path - /opt/lampp/htdocs/..... I don't know if it's ideal, but it's working for now
  19. Thanks. I added a line in the config file to echo 'connected to db' and I was seeing that line echoed so thought I was connecting fine. All the error messages told me otherwise. I'm still experiencing some issues with the config.php file though. I'm using XAMPP and my relevant dir structure is: -htdocs |-/site | - /assets |- files you'd expect to find here | - /loginphp | - register.php | - /handlers | - register_handler.php | - config | - config.php It looks to me like the correct relative path from register_handler.php is ../../config/config.php (up 2 directories and into config/config.php but I get the following error - Warning: require_once(/qcic/config/config.php): Failed to open stream: No such file or directory in /opt/lampp/htdocs/site/loginphp/register.php on line 2 Fatal error: Uncaught Error: Failed opening required '/qcic/config/config.php' (include_path='.:/opt/lampp/lib/php') in /opt/lampp/htdocs/site/loginphp/register.php:2 Stack trace: #0 {main} thrown in /opt/lampp/htdocs/site/loginphp/register.php on line 2 I've tried every relative path from '../../config/.....', '../config/........', etc I couldn't even get the absolute path (/site/config/config.php) to work. Watched a few videos on config.php files, couldn't find anything useful. I feel like this is one of those really simple things that my inexperience is stopping me from noticing. Could you shine any light on what I'm doing incorrectly or need to be doing MORE correctly?
  20. This was solved by simply replacing require() with include() which brings more questions. I was under the impression that non critical files should use include and require should be used for critical matters. In my above reg form the config.php file is definitely critical but I get a blank screen after submitting the data if I use require here and everything is fine using include. Why?
  21. I have the following code for a very simple registration form handler - //require('../../config/config.php'); require('reg_functions.php'); //assign clean form variables $firstname = clean_names($_POST['firstname']); $lastname = clean_names($_POST['lastname']); $username = clean_names($_POST['username']); $email = clean_email($_POST['email']); $email2 = clean_email($_POST['email2']); $password = clean_password($_POST['password']); $password2 = clean_password($_POST['password2']); $date = date("Y-m-d"); if(isset($_POST['register_button'])) { //validate & confirm emails match if($email == $email2) { if(filter_var($email, FILTER_VALIDATE_EMAIL)) { $email = filter_var($email, FILTER_VALIDATE_EMAIL); // I THINK I COULD DEF MAKE THIS EMAIL CHECK CLEANER - WORK ON THAT LATER //check if email exists in system already $email_check = mysqli_query($conn, "SELECT email FROM users WHERE email='$email'"); //number of rows returned with the email (this should be 0 to proceed) $number_rows = mysqli_num_rows($email_check); if($number_rows > 0) { echo "Email already in use <br>"; } else { echo "email is correct <br>"; } } else { echo "Invalid email format <br>"; } } else { echo "Emails don't match <br>"; } echo "First Name: " . $firstname . "<br>"; echo "Last Name: " . $lastname . "<br>"; echo "Username: " . $username . "<br>"; echo "Number of Rows: " . $number_rows . "<br>"; } the code that starts with the mysqli_query() function to check for matching emails is where it fails (white screen). I figured it may be because the $conn variable wasn't being associated with the $conn variable in the config.php file. So I added the require config.php at the top. Now it only works if both are commented out. So in reality this could be 2 issues and I struggled with how not to ask too many questions in one post though they may be the same issue. I'm really new to php. Can someone explain the mechanics of whats going on (or not going on). to recap, this is the code that caused the white screen initially - // I THINK I COULD DEF MAKE THIS EMAIL CHECK CLEANER - WORK ON THAT LATER //check if email exists in system already $email_check = mysqli_query($conn, "SELECT email FROM users WHERE email='$email'"); //number of rows returned with the email (this should be 0 to proceed) $number_rows = mysqli_num_rows($email_check); if($number_rows > 0) { echo "Email already in use <br>"; } else { echo "email is correct <br>"; } and I realized it had no association to the $conn variable in the config.php file so I added the require config.php line at the top. This above code is obviously still not working and if I comment it out, the require config.php line still causes a white screen. This is my config file - ob_start(); session_start(); $timezone = date_default_timezone_set("America/xxxxxxxx"); $db_host = "localhost"; $db_user = "root"; $db_pass = ""; $db_name = "qcic"; $conn = mysqli_connect( $db_host, $db_user, $db_pass, $db_name ); if( $conn->connect_errno) { printf( "Connect Failed: %s\n", $conn->connect_error); exit(); } else { echo "Connected to database"; } ob_end_flush(); and I get the "Connected to database" echo so I have no idea what the problem is. Anyone with some time and more experience that can explain some of these things? thanks
  22. That's really awesome, requinix. I would never have thought to do it that way. This thread really helped me think differently about things. I appreciate the help. I hope I'll be able to contribute knowledge here someday too. Until then I'm sure I'll have more questions. Thanks again Out of curiosity, how would you do it using a foreach loop. I mean, how would you have moved $name into $firstname, $lastname, $username with a foreach loop and a function?
  23. gw1500se, thank you, you've helped me think like a better programmer This is making more sense to me. I was looking at this part and wondering how $name 'got into' $firstname, $lastname etc. and felt this was a weak point. I had this code initially (I'm very new at PHP): //registration form values // //first name // $firstname = strip_tags($_POST['firstname']); //remove html tags // $firstname = str_replace(' ', '', $firstname); //remove spaces // $firstname = ucfirst(strtolower($firstname)); // //last name // $lastname = strip_tags($_POST['lastname']); //remove html tags // $lastname = str_replace(' ', '', $lastname); //remove spaces // $lastname = ucfirst(strtolower($lastname)); // //user name // $username = strip_tags($_POST['username']); //remove html tags // $username = str_replace(' ', '', $username); //remove spaces // $username = ucfirst(strtolower($username)); but it's so redundant it was a prime candidate for a function. I want to become much better at coding and am really interested in exposing myself to different approaches to the same challenge. So how would you do this? What do you feel is the tightest, most efficient way of making this happen?
  24. Ok, thanks for the replies. The quoted out return in the function was a mistake from editing the copy/paste that I didn't catch before posting, it's not really in the code. Concerning how functions return values, that means that theoretically the following amended code should work: function clean_names($dirty_name) { $dirty_name = strip_tags($dirty_name); $dirty_name = str_replace(' ', '', $dirty_name); $dirty_name = ucfirst(strtolower($dirty_name)); return $dirty_name; } $names = array($firstname, $lastname, $username); if(isset($_POST['register_button'])) { // WHY IS THIS FUNC CALL NOT WORKING?? foreach($names as $name) { $name = clean_names($name); } echo "First Name: " . $firstname . "<br>"; echo "Last Name: " . $lastname . "<br>"; echo "Username: " . $username . "<br>"; } To my inexperienced eye it looks like it should work. It seems so simple but it's not working. I feel like it's some small, stupid oversight from me. I've watched 2 videos and skimmed the functions sections of 3 tuts hoping to get a cue that would open my eyes to the issue. I didn't. What's the issue here?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.