PHP - Php Configuration Functions
Greetings. First time poster looking for some PHP help if it can be provided, because I'm confused as hell.
First off, I'll explain my situation. I'm the IT technician of a small business tasked with ensuring the business' computers and website are running in top shape. I originally joined as a Data Entry clerk to post products to the business' website, but got promoted when they original IT technician left town to pursue his PhD. Before he left, he taught me a few things to prepare watching over the website as my new duty, including some PHP and MySQL. Now, a few months after he's left, I have the glorious task of trying to move our website to a new host because we've apparently outgrown our original host's capabilities. And being based on PHP with a MySQL database, it's turning out to be OH-so-fun. So far, I've managed to download our entire website from the old host and given it to our new host to put up as a testing site, to ensure everything works before we switch hosts. And for the most part, a lot of it has come together nicely, but there's still work needed to be done. And thus we come to the problem I hope someone here can help with. Our website has two back-end Admin sections, an older one that used to handle everything, and a newer one that's sleeker but still reads off a lot of data from the older one to work properly. Normally, when you access those sections, you need to log in first. Now, in the testing site, when I access those areas, the header of each of those sections now reads <?= $row[title] ?> and if I attempt to log in as normal, it just flashes back to the login page with the fields now cleared of what I typed in. When I asked about this to see what the problem might be, the new host said it might "at least two misconfigurations on the new server relative to the old server". So, I've been trying to Google for some help with PHP Configuration Functions, but I don't know if what I'm reading will help me fix the problem. Would anyone here happen to know what they might mean in this case, because my knowledge of PHP is fairly limited beyond what I was taught to keep the website in order without considering a host change... Thanks to those who help or can direct me to a place that helps! Similar TutorialsDear All, hi, i have a configuration file with this content for example: test = "100"; teste2 = "300"; now need change the test from 100 to 300, i just want use something like this: function ( "test" , 300 ); how can i do that? thanks for help Hi Guys, I'm a bit new to php coding, so dont really know what the best way to code this up. I want to make a router configuration page, such as it shows the link status (ip/gatewatway etc) and also have input selections such as to turn on/off wifi / dhcp etc. Ideally I would like the data stored and written to a text file as the php server is running on a linux arm embedded system. At present I have been able to read back from a text file the corresponding values for each of the status information (these are stored on a seperate line), but I'm having difficulty writing and reading the input selection boxes, such as when the dhcp is disabled I need it to write to a file that its been disabled and refresh the page to allow entry of the static ip box and then when the apply button is pressed the value is written to the file. This may be out of my depth, hopefully someone could give me some pointers. I feel that the text file might not be the best way of storing and recalling values but I don't know of another way. Many Thanks Tim Hi ,
My site gives a blank page when i click on an article due to insufficient memory. I tried making changes to the php.ini file but the changes are not getting reflected.
Please advice if a server restart is needed for this, how do i go about it?
Thanks in advance
Does any know how to set SMTP Mail Configuration using the gmail account? Every now and then, I experience some strange behavior and eventually trace it back to parse_ini_file's scanner_mode being applicable to some parameter but not another, and am looking for alternatives. Below are several of my thoughts. How do you store configuration settings? Do you use different approaches based on the application, and if so what criteria governs which you use? Keep with a ini file with parse_ini_file. Obviously, not working for me. An ini file but with a class dedicated to ensuring the data is appropriate. Seems like too much duplicating content which will result in errors. YAML. Don't think I want to. XML. Not as readable. In a database. Maybe, but might be harder to manage. Hardcode an array in PHP. Probably not. JSON. I like the idea, but feel comments are important in a config file, and am considering the following: Add extra valid JSON properties which contain comments. Don't like the idea. Use JavaScript's JSON.stringify. Too much mixing technologies. Add comments to the JSON and then strip them using a 3rd party parser such as https://json5.org/ or a little regex. My main issue is inability to auto-format, but this seems viable. Any other ideas?Thanks!
I have a configuration file, and I use parse_ini_file() to parse it into an array at the initial entry point of my script.
I would like the settings to be available to all downstream scripts/methods/functions. Note that my intent is not to change them outside of the configuration file, and ideally they will be readonly, however, if not, I suppose that is okay and I will just be sure not to modify them.
I've read about dependency injection, and while this mostly makes sense, it seems like a lot of script/troubleshooting for little if any value for this scenario.
I've read that global variables are bad (couples your objects to some "god" object, cannot unit test, difficult to troubleshoot, difficult to work with multiple developers, potential conflicts with other classes, namespace pollution), but maybe not for this case?
Another option is defining a bunch of constants to the appropriate values, but this seems redundant.
Or maybe some static class with all the values?
What would be the best way to make configuration settings available to all PHP scripts, functions, and methods? Hi, I've been googling for a while now for an example of how to read, edit and write a configuration file. This part was easy to find... <?php $icon_settings = parse_ini_file("test.desktop"); print_r($icon_settings); ?> ...the above code works great, but how do I edit and write back a configuration file like this... Code: [Select] #!/usr/bin/env xdg-open [Desktop Entry] Version=1.0 Name=Firefox Web Browser Comment=Browse the World Wide Web GenericName=Web Browser Exec=firefox %u Terminal=false X-MultipleArgs=false Type=Application Icon=firefox # ... and so on back to the drive? Thanks. Hi, I don't know if this is even possible, but I need to copy configuration of existing server to my localhost (using XAMPP on windows). I only have full output of phpinfo() function from that server (I don't have access to php.ini), and I would like to change my localhost server to behave exactly like remote one.
Is this even possible? Hey Guys, First Post! Yay I have a problemo, I feel really stupid about having to ask this question. But basically I can't get a new line when I echo output in a script I am writing for my website. Here is the code: echo $newsentry; echo '_______ Read More..'; I tried '\n' I tried using " (double quotes) I have used ' (single quotes) I have positioned the Line Feed '\n' at the begining of an output, and at the end. Neither made any difference. I tried using carriage return '\r' too. Nothing. I haven't coded in PHP for a while, well nothing basic (I know that sounds rediculas but it has been mostly Apps etc, Client Scripts blah blah blah, but I haven't had to echo with a new line for a few years. Probably the last website which I wrote with my friend. So I checked the PHP manual, and I am clearly missing something when I am reading it, I understood it to mean (which as you can see from my code, is what I am currently doing), that you could just somehow type: echo "some string and it carries on to the second line"; However as I first thought, this makes no difference. Could this be a configuration on the Box that hosts this Site?? Sorry for sounding so stupid guys, I appreciate your help. -Craig This topic has been moved to PHP Installation & Configuration. http://www.phpfreaks.com/forums/index.php?topic=326567.0 I have a script I am putting together that simulate a cricket game. The only issue is, that there are a huge number of functions because there doesn't seem to be any other way to do this properly. As well as this, there a while() loop and all this seems to be leading to the page reaching a max 30 second timeout when generating the result. My code is attached below, it is quite messy at the moment because i've just be working on it, but I was wondering if anyone has any solutions of how I can speed this up or change to prevent a timeout: <?php // Error reporting error_reporting(E_ALL); // Connect DB mysql_connect("wickettowicket.adminfuel.com", "rockinaway", "preetha6488") or die(mysql_error()); // Select DB mysql_select_db("wickettowicket") or die(mysql_error()); // MySQL queries to find batsmen and bowlers $array_batsmen = mysql_query('SELECT id, name, ability, strength FROM wtw_players WHERE team_id = 1 ORDER BY id ASC'); $array_bowlers = mysql_query('SELECT id, name, ability, strength FROM wtw_players WHERE team_id = 2'); // Start table for data $data = '<table width="600px">'; // Create blank scorecard while ($array_bat = mysql_fetch_array($array_batsmen)) { $data .= '<tr><td>'.$array_bat['name'].'</td><td></td><td></td><td>0</td></tr>'; } // Set up arrays for players $current_batsman = $current_bowler = array(); // Reset query mysql_data_seek($array_batsmen,0); $in_one = $in_two = $it = ''; function currentBatsman($id, $name, $ability, $strength, $out, $in, $runs) { global $current_batsman; $current_batsman = array ( 'id' => $id, 'name' => $name, 'ability' => $ability, 'strength' => $strength, 'out' => $out, 'in' => $in, 'runs' => $runs ); echo 'set current'; } // Set up arrays of batsmen while ($array = mysql_fetch_array($array_batsmen)) { if ($it < 3 && $in_one == '') { currentBatsman($array['id'], $array['name'], $array['ability'], $array['strength'], 0, 1, 0); $batsmen[$array['id']] = array ( 'id' => $array['id'], 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'], 'out' => 0, 'in' => 1, 'runs' => 0 ); $in_one = $array['id']; $current = $array['id']; $it++; } else if ($it < 3 && $in_two == '') { $batsmen[$array['id']] = array ( 'id' => $array['id'], 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'], 'out' => 0, 'in' => 1, 'runs' => 0 ); $in_two = $array['id']; $it++; } else { $batsmen[$array['id']] = array ( 'id' => $array['id'], 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'], 'out' => 0, 'in' => 0, 'runs' => 0 ); } } // Bowler Array while ($array = mysql_fetch_array($array_bowlers)) { $bowlers[] = array ( 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'] ); } // Reset both queries mysql_data_seek($array_bowlers,0); mysql_data_seek($array_batsmen,0); function changeBatsman($just_out) { global $array_batsmen, $batsmen; //Update array $batsmen[$just_out] = array ( 'in' => 1, 'out' => 1 ); while ($array = mysql_fetch_array($array_batsmen)) { if ($just_out != $array['id'] && $batsmen[$array['id']]['out'] != 0) currentBatsman($array['id'], $array['name'], $array['ability'], $array['strength'], 0, 1, 0); } // Reset query mysql_data_seek($array_batsmen,0); echo 'change batsman'; } function swapBatsman($other_batsman) { global $array_batsmen, $batsman; while ($array = mysql_fetch_array($array_batsmen)) { if ($other_batsman != $array['id'] && $batsman[$array['id']]['out'] != 0 && $batsman[$array['id']]['in'] == 1) currentBatsman($array['id'], $array['name'], $array['ability'], $array['strength'], 0, 1, 0); } // Reset query mysql_data_seek($array_batsmen,0); echo 'swap batsman'; } $runs = $outs = $balls = $overs = 0; $played = array(); function selectBowler() { global $bowlers, $current_bowler; // Select random bowler $choose_bowler = array_rand($bowlers, 1); $current_bowler = array ( 'name' => $bowlers[$choose_bowler]['name'], 'ability' => $bowlers[$choose_bowler]['ability'], 'strength' => $bowlers[$choose_bowler]['strength'] ); } /* function selectBatsman(); { global $array_batsmen; while ($array_batsmen[]['out'] != 1) { }*/ function bowl() { global $batsmen, $bowlers, $current_bowler, $current_batsman, $data, $balls, $outs, $runs; if ($current_batsman['out'] == 0) { echo 'bowling'; // Set the initial number $number = rand(0, 190); // Ability of batsman if ($current_batsman['ability'] > 90) $number += 30; else if ($current_batsman['ability'] > 70) $number += 15; else if ($current_batsman['ability'] > 50) $number += 2; else $number = $number; // Strength of batsman if ($current_batsman['strength'] > 90) $number += 15; else if ($current_batsman['strength'] > 70) $number += 10; else if ($current_batsman['strength'] > 50) $number += 5; else $number = $number; // Depending on overs if ($balls > 270) $number += 30; else if ($balls > 120) $number -= 10; // Ability if ($current_bowler['ability'] > 90) $number -= 30; else if ($current_bowler['ability'] > 70) $number -= 15; else if ($current_bowler['ability'] > 50) $number -= 2; else $number = $number; // If batsman has made a huge total of runs, we need to knock some numbers off - more likely to get out if ($current_batsman['runs'] > 200) $number -= 70; else if ($current_batsman['runs'] > 100) $number -= 30; // Finally sort out runs if ($number > 190) $run = 6; else if ($number > 170) $run = 4; else if ($number > 160) $run = 3; else if ($number > 100) $run = 2; else if ($number > 50) $run = 1; else if ($number > 10) $run = 0; else if ($balls > 120 && $number > 0) $run = 0; else $run = -1; // Increase number of balls $balls += 1; // Are they out? if ($run == -1) { $current_batsman['out'] = 1; $played[] = $current_batsman['id']; $find = '<tr><td>'.$current_batsman['name'].'</td><td></td><td></td><td>0</td></tr>'; $replace = '<tr><td>'.$current_batsman['name'].'</td><td></td><td>'.$current_bowler['name'].'</td><td>'.$current_batsman['runs'].'</td></tr>'; $data = str_replace($find, $replace, $data); changeBatsman($current_batsman['id']); echo 'out'; } else { $current_batsman['runs'] += $run; $runs += $run; if ($run == 1 || $run == 3) { swapBatsman($current_batsman['id']); echo 'time to swap'; } echo $run; } // Count outs if ($current_batsman['out'] == 1) $outs += 1; } } function game() { global $main, $batsmen, $bowlers, $data, $batted, $balls, $outs, $current_batsman; // Check if possible while ($balls <= 295 && $outs < 10) { selectBowler(); // Actually bowl now bowl(); } } game(); echo $data; I teaching myself php, but I am coming from java and other compiled languages, so the process has been a little bumpy. I am trying to do something like this: Code: [Select] class my_class { function one () { $two = two (); $three = three (); $five = $two + $three; return $five; } function two () { $two = 2; return $two; } function three () { $three = 3; return $three; } } Unfortunately, I keep getting an error message saying that my call to two () is an undefined function. I am gathering from this that the scope of one () is not aware of the existence of two (). Is there a way to get around this so I can call two () and three () from one ()? When in OOP I have been watching tutorials and some have functions just written like Code: [Select] class user { public $user; public function __construct($u) { $this->user = $u; } } do I need to type public before the function or is it better practice to just put Code: [Select] class user { public $user; function __construct($u) { $this->user = $u; } } Thanks! I have a function to get some data from the database. However it always comes back with: Code: [Select] Notice: Undefined variable: db in C:\wamp\www\ASF A Simple Forum\functions\functions.php on line 233 Fatal error: Call to a member function query() on a non-object in C:\wamp\www\ASF A Simple Forum\functions\functions.php on line 233 here is the code: require_once(ASF_ROOT."includes/init.php"); // this is where the db connection is function get_db_info($type, $select, $table, $where="", $order="DESC", $limit="") { if (empty($type) || empty($select) || empty($table)) { try { throw new emptyArgs(); } catch (emptyArgs $invalid) { echo "<div style=\"border:1px solid #f00; padding:5px; width:350px; line-height:10px;\">"; echo "<b>$invalid</b>"; echo "<p>get_info() needs atleast 3 arguments (Type, Select and Table) - none supplied</p>"; echo "</div>"; } } else { if ($type == "num") { $query = $db->query(" SELECT $select FROM ".TBL_PREFIX."$table $where $order $limit ") or die($db->error()); $result = $db->num_rows(MYSQLI_NUM); return $result; } else { $query = $db->query(" SELECT $select FROM ".TBL_PREFIX."$table $where $order $limit "); $result = $query->fetch_array(MYSQL_ASSOC); return $result; } } } I have a function for mysqli_real_escape_string above this function and it works with no errors. Have i done something wrong? as you can see i am including the file which holds the connection but it doesnt seem to use it. every other file that uses the connection works fine. i am wondering what the '@' is used for when used in front of a function like Function(@$VAR); What is the benefit of the @? Hey guys. Can anyone tell me what this means? function foo { } It is basic php but I don't get it. Should it be called for execution, or does it work automatically, or both? The php manual at w3schools and php.net are too tough for me. That's why I posted here. Thanks! Hi, I've got 3 different upload forms in one form. I'm making a function so it just one piece of code. I'm having a bit of a problem changing some of the code so it works for all 3. Here is the function function loadImages ($action, $position) { $link = mysql_connect("","",""); if (!$link) { die('Error: could not connect: ' . mysql_error()); } $db_select = mysql_select_db("",$link); if (!$db_select) { die ("Error: database selection failed: " . mysql_error()); } $page = ($_POST["pageName"]); if ((($_FILES["bfile"]["type"] == "image/gif") || ($_FILES["bfile"]["type"] == "image/jpeg") || ($_FILES["bfile"]["type"] == "image/pjpeg")) && ($_FILES["bfile"]["size"] < 9000000)) { if ($_FILES["bfile"]["error"] > 0) { echo "Return Code: " . $_FILES["bfile"]["error"] . "<br />"; } else { $success = '<p style="font-weight: bold; color: #C00">Upload successful<br />'; // move uploaded temporary file to final destination move_uploaded_file($_FILES["bfile"]["tmp_name"], "uploads/" . $_FILES["bfile"]["name"]); // update database with new filename $filename = $_FILES["bfile"]["name"]; $sql = "UPDATE images SET filename= '$filename' where page = '$page' and position = '$position'"; if (!mysql_query($sql,$link)) { die('Error: ' . mysql_error()); } } } else { $fail = '<p style="font-weight: bold; color: #C00">Invalid file'; } mysql_close($link); } Now the $action is the bit that will determine whether it's updateTop updateMiddle or updateBottom. $position finds out if it's top middle or bottom if (isset($_POST['updateTop'])) { loadImages ($_POST['updateTop'], "top"); } Here is a sample of the html, it's in a switch case "Products": echo "<p>Image Top</p> <input type=\"file\" name=\"file\" id=\"file\" /> <input type=\"submit\" name=\"updateTop\" value=\"Update $page Top\" /> <p>Image Middle</p> <input type=\"file\" name=\"mfile\" id=\"mfile\" /> <input type=\"submit\" name=\"updateMiddle\" value=\"Update $page Middle\" /> <p>Image Bottom</p> <input type=\"file\" name=\"bfile\" id=\"bfile\" /> <input type=\"submit\" name=\"updateBottom\" value=\"Update $page Bottom\" /> "; break; What I need to do is get $_FILES["bfile" into a variable to see whether it's "bfile" "mfile" or "file" but not sure how to write it. Thanks for the help. Hi, I could use a little guidance here. 1) is a function with values to be sent to 2). 1) <!-- use ajax - OnCalc function to send data to your database. --> function OnCalc(value1,op,value2,total) { 2) php file using above function values <?php header( "refresh:5;url='http://localhost/hom...lcprint.php'"); echo 'You\'ll be redirected in about 5 secs. If not, click <a href="http://localhost/hom...hp">here</a>.'; echo '<script type="text/javascript">' , 'OnCalc();' , '</script>'; No error but code not working? Can someone tell me by looking at the following code why function pcust() does not work, I am new to programming? Code: [Select] $firstname = "chris"; $surname = "reynolds"; $address1 = "12 birch end"; $town = "Wallington"; $county = "surrey"; $postcode = "wh20 3bg"; $telephone = "01372 854785"; $cust_details = fix_cust($firstname, $surname, $address1,$town, $county, $postcode, $telephone ); function fix_cust($n1, $n2, $n3, $n4, $n5, $n6, $n7) { $n1 = ucfirst(strtolower($n1)); $n2 = ucfirst(strtolower($n2)); $n3 = ucwords(strtolower($n3)); $n4 = ucfirst(strtolower($n4)); $n5 = ucfirst(strtolower($n5)); $n6 = ucfirst(strtoupper($n6)); $n7 = ucfirst(strtolower($n7)); return array($n1, $n2, $n3, $n4, $n5, $n6, $n7); } echo $cust_details[0] . " " . $cust_details[1]; echo "<br />"; echo $cust_details[2]; echo "<br />"; echo $cust_details[3]; echo "<br />"; echo $cust_details[4]; echo "<br />"; echo $cust_details[5]; echo "<br />"; echo $cust_details[6]; echo "<br />"; function pcust(){ $length = count($cust_details ); for ($i = 0; $i < $length; $i++) { echo $cust_details[$i]; echo "<br />"; } } pcust(); |