PHP - Need Advice On How To Debug In The Middle Of Code
I need some advice on how to debug in the middle of other code
I want to change a file. This file gives no direct output and contain a load of functions What I want to do is make changes to this file and trace values - how do I do this? - Can I give a popup with a message? - Could I write values to a file? (Can I write to some sort of standard log file? Maybe the error log file?) - Or is it possible to still give output to a web page, even though the code is not part of the code that outputs to the browser? What's the standard practice of debugging as mentioned above? Any help would be really appreciated Thanks! OM Similar TutorialsI'm stuck trying to figure out why when I make a selection in drop down list of a php form, the selection does not stay when form is refreshed or re-displayed due to an pattern error of another field. My array is state_province = array(list of the states, and provinces) field name in the label array is: "state" => "State", Here is the code that I'm using: echo "<form action='$_SERVER[PHP_SELF]' method='POST'>"; foreach($labels as $field => $label) { if($field == "state") { echo "<div class='province_state'><label for='state' size='15'>* Province/State</label><select>"; foreach($state_province as $state) { echo "<option value=$state"; if(@$_POST['state'] == $state) { echo "selected='selected'"; } echo ">"; echo $state; echo "</option>\n"; } echo "</select></div>\n"; } I am new to PHP and trying to debug some old code. How am I to read the following: $properties = $GLOBALS['Props']; I am guesisin that it is saying: Create a string variable named $porperties and set it equal to the Global variable of 'Props'. But I assume my interpretation is wrong since I get some weird results. I have made a bit of code that queries the database for articles published within the last year. <?php echo $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND 'post_date' > '" . date("Y") . "-01-01-01 00:00:00'"); if (0 < $numposts) $numposts = number_format($numposts); ?> I really don't even know how it works and need to because I want to figure out how to make it also get articles published within the last day, month and week. I suppose if I understood what needs changing I might be able to work with it but so far everything I've tried either returns the exact same number or nothing at all. It's for Wordpress just to note. Any ideas? We have a 10 year old system that has 100's of php files that all connect to a central database using a single shared php include file that does a mysql_connect before any other code executes
include_once("db_connect.php");
mysql_query(...);
We never used the returned connection id (link identifier) from the mysql_connect function call as it was always implied in every subsequent mysql_query statement etc.
Now we've moved some of our bigger tables to a separate server, so we have to update small areas of some files that need to connect to this other system and send the old existing query to the new server instead of to the main server and process the results
So, going back through hundreds of files and converting mysql_query($string) to mysql_query($string, $connection1) is not practical
Possible solution one: anytime I need to use the new server I do this
------------------------------------------------------------------------------
mysql_connect(NEW_SERVER_IP,"...",""");
mysql_query($string);
...
// get back to normal
mysql_connect(OLD_SERVER_IP,"...","");
Possible solution two: modify the master include file
------------------------------------------------------------------------------
//add a new line ABOVE the existing mysql_connect statement...
$new_server=mysql_connect(NEW_SERVER_IP,"...",""");
mysql_connect(OLD_SERVER_IP,"...","");
and then anytime I need to use the new database I have to go back and modify all of the mysql_query,mysql_affected_rows, etc to reference the $new_server link identifier
solution one seems much simpler, but all of the mysql_connect reconnections I assume will be extremely inefficient
solution two seems cleaner but if I miss one mysql_ statement that needs the new connection object as a reference, the code will completely break or worse yet it will produce unexpected results
Example: If I miss converting a mysql_errno() function that really needs to now be mysql_errno($new_server) it would cause major issues.
So, I'm wondering if others have faced this dilema of migrating single mysql_connect code to multi mysql_connect mode and if there is a better way of going about implementing it.
Thanks in advance for any help
Steve
I wanted to develop a Wordpress plugin. There's a few existing plugins for the subject I wanted to code. I thought I would get some inspiration by looking at the code. It's a gigantic mess of spaghetti! The code I looked at, looks like it's been made using an IDE. There seems to be so many folders and files - it's crazy. Others I looked at seem to be similar. It seems like an enterprise application. I'm more than capable of writing a spec and coding myself. The only thing I'm missing would be Wordpress practices - setting up tables and using hooks and other things. Any advice? Or do I just get dirty and sink my head into the code and untangle? QUESTIONS: - Do you use an IDE to code PHP? - What are the most popular IDE's? Thanks. I need help debugging some homework.I have narrowed the problem down to the switch. when it gets to the switch it is spitting out the default everytime, also in the default the $score in both echo statment is not showing. Its probably something small killing me I just can't see it and dont have enough experience with php yet.
<body> <?php $faceNamesSingular = array("one", "two", "three", "four", "five", "six"); $faceNamesPlural = array("ones", "twos", "threes", "fours", "fives", "sixes"); function checkForDoubles($die1, $die2) { global $faceNamesSingular; global $faceNamesPlural; $returnValue = false; if ($die1 == $die2) { //Doubles echo "The roll was double ", $faceNamesPlural[$die1-1], ".<br />"; $returnValue = true; } else { // Not Doubles echo "The roll was a ", $faceNamesSingular[$die1-1], " and a ", $faceNamesSingular[$die2-1], ".<br />"; $returnValue = false; } return $returnValue; } function displayScoreText($scores, $doubles) { switch ($score) { case 2: echo "You rolled snake eyes!<br />"; break; case 3: echo "You rolled a loose deuce!<br />"; break; case 5: echo "You rolled a fever five!<br />"; break; case 7: echo "You rolled a natural!<br />"; break; case 9: echo "You rolled a nina!<br />"; break; case 11: echo "You rolled a yo!<br />"; break; case 12: echo "You rolled boxcars!<br />"; break; default: if ($score % 2 == 0) {//any even number if ($doubles) { echo "You rolled a hard $score!<br />"; } else { //Not Doubles echo "You rolled an easy $score!<br />"; } } break; } } $dice = array(); $dice[0] = rand(1,6); $dice[1] = rand(1,6); $score = $dice[0] + $dice[1]; echo "<p>"; echo "The total score for the roll was $score.<br />"; $doubles = checkForDoubles($dice[0], $dice[1]); displayScoreText($score, $doubles); echo "</p>"; ?> </body> </html>Attached Files DiceRoll.php 1.94KB 0 downloads Here is my hierarchy: data -logs folder-debug.log htdocs -index.php include -Contollers,Smarty,Templater,Zend templates Now when I launch index.php from htdocs, I get this un-seeming error: "Fatal error: Uncaught exception 'Zend_Log_Exception' with message '"/var/htdocs/Books/practical_Web2.0/data/logs/debug.log" cannot be opened with mode "a"' in C:\xampp\php\PEAR\Zend\Log\Writer\Stream.php:69 Stack trace: #0 C:\xampp\htdocs\Books\practical_Web2.0\chapter-02\htdocs\index.php(11): Zend_Log_Writer_Stream->__construct('/var/htdocs/Boo...') #1 {main} thrown in C:\xampp\php\PEAR\Zend\Log\Writer\Stream.php on line 69 " I went to the zend folder in xampp, php, pear, zend, log, writer.php dir and I found this: /** * Class Constructor * * @param streamOrUrl Stream or URL to open as a stream * @param mode Mode, only applicable if a URL is given */ public function __construct($streamOrUrl, $mode = 'a') { if (is_resource($streamOrUrl)) { if (get_resource_type($streamOrUrl) != 'stream') { require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception('Resource is not a stream'); } if ($mode != 'a') { require_once 'Zend/Log/Exception.php'; throw new Zend_Log_Exception('Mode cannot be changed on existing streams'); } $this->_stream = $streamOrUrl; } else { if (! $this->_stream = @fopen($streamOrUrl, $mode, false)) { require_once 'Zend/Log/Exception.php'; $msg = "\"$streamOrUrl\" cannot be opened with mode \"$mode\""; throw new Zend_Log_Exception($msg); } } $this->_formatter = new Zend_Log_Formatter_Simple(); } line 69 is =>throw new Zend_Log_Exception($msg); What could be wrong? Hint: settings.conf [development] database.type = pdo_mysql database.hostname = localhost database.username = root database.password = database.database = phpweb20 paths.base = /var/htdocs/Books/practical_Web2.0 paths.data = /var/htdocs/Books/practical_Web2.0/data paths.templates = /var/htdocs/Books/practical_Web2.0/templates logging.file = /var/htdocs/Books/practical_Web2.0/data/logs/debug.log Any pointer as to why there is a fatal error? Hi all, Well new to the forum. And New to PHP. I have a quick question. I'm trying to figure out what the best pratice would be to get familier with a php installation. More specificly how to discover what php is specificly producing a specific part of the final rendered HTML. I have a MyBB Forum me and my cuzin are setting up and for the life of me i can find the right place to add our own HTML to change a specific Div in the Final page output. here is the link to our URL http://destroyrebuilduntilgodshows.net/forum/ I specificly want to find out where i can edit the contents of the "wrapper_container" Div. I have searched for the source on my local machine. but all i can find is the CSS responsible for the styling and as it turns out the images being held in that div. My end goal is to replace the "advertise here" image with come JS that does our ad managment. So, I guess my question is How would you find it. How would you find the responsible php for that section of the HTML. Thanks All. Cheers I don't know how to debug this. The if is being executed and I want to have the else be executed. How can I find out what $_POST['id'] is? Code: [Select] if (isset($_POST['id']) === false) { echo 'Invalid user id.'; } else { partner_request($_POST['id']); echo "A request has been sent to the user"; } I have a simple INSERT statement that isn't inserting anything into one of the columns: coin_name I've gone over everything and can't figure out why. All other columns get data. HTML form passes everything fine. Is there a good way to debug this? I've triple checked everything and there are no type-o's that I see. Driving me mad... $query = "INSERT INTO Coin (coin_name, coin_value, coin_condition, year_minted, face_value, purchase_price) VALUES ('$coin_name', '$coin_value', '$coin_condition', '$year_minted', '$face_value', '$purchase_price');"; I'm trying to debug a sporadic 500 error which users occasionally get when filling out form sections on my site. Whenever it happens, I try to recreate their environment and fill out the form exactly as they have done, but can never recreate the issue. I have checked my application logs for the framework I am using, but they report nothing related to the error. I'm hoping someone here might be able to help me catch this. This is where the 500 error is sometimes happening and this triggers the error which users report: Code: [Select] //In jquery AJAX function, which submits data to PHP form for processing error: function(data,transport){ $.validationEngine.debug("error in ajax response: "+data.status+" "+transport) } Is there a way for me to try and catch what the exact error is here? The problem is I can't recreate the error, either using an AJAX call or not to call the PHP script. I need to catch the error response from the PHP script being called, but I never get an error when trying to recreate the user issue, i.e same OS/browser/form answers. The Apache error logs for the site have very little - unless I'm looking in the wrong place. I'm on CentOS, ran locate error_log and got an error_log.txt file in my vhosts directory for my site, but this just contained a 302 message log, nothing else. I also tried locate error | grep mysite. I'm a new PHP users needing some help. I've got two code snippets that run a CURL post to a syslog server grabbing specific records that I then parse with regex. Both are similar differing in the specific CURL URL and Regex. One works does not. The failing program seems to fail the regex parse creating a null array. What I've done thus far. Tested the regex on regex101 (works fine, using PCRE). Dumped and echoed the CURL created variable and then rechecked against regex101. ( again all good) Created a static variable in the code of the CURL return string (regex works fine). So the CURL is working, the variable string is working appearing as expected and the regex works. But the variable feels like's it null when passed directly from the CURL to the regex. No error logs, but this is where I'm stuck I don't know what to pick at next. Here's a version of the code. Appreciate any thoughts. <?php
$str = curl_exec($ch); // curl steps ends
//$str = '_bkt _cd _indextime _kv _raw _serial _si _sourcetype _time dhcp_type host index linecount source sourcetype splunk_server Configuration initialization for /opt/splunk/etc took 17ms when dispatching a search (search ID: 1591198882.902292) Your search was restricted by ( ( index=nat OR index=network OR index=radius ) OR ( source=/data/syslog/security/ipblocker ) ) base lispy: [ AND 66 82 8a 9c dhcpack e4 f8 sourcetype::infoblox:dhcp [ OR index::nat index::network index::radius source::/data/syslog/security/ipblocker ] ] search context: user="testuser", app="search", bs-pathname="/opt/splunk/etc" Your timerange was substituted based on your search string network~978~07708B5A-37B1-4315-8EFA-70B96D12856C 978:118251396 1591198398 1 Jun 3 11:33:15 128.230.100.36 dhcpd[21353]: DHCPACK on 10.1.0.19 to e4:f8:9c:82:66:8a (ITS-NDD-BOA-T01) via bond0 relay bond0 lease-duration 7200 0 its-splunk-idx2.syr.edu network infoblox:dhcp 2020-06-03 11:33:15.000 EDT DHCPACK 128.230.100.36 network 1 /data/syslog/network/dhcp infoblox:dhcp its-splunk-idx2.syr.edu'; So I have been printing out some debug information, and now I am trying to instead save it to a string, which I will print out at the end of the page. Here is some specific problematic code: $myplugin_debug_test.= "\n[GETTING COOKIES...\n"; $myplugin_debug_test.= print_r($myplugin_all_cookies);//THIS IS LINE 38 $myplugin_debug_test.= "\n... DONE]<br/>\n"; However, when I do this I get the following error: Quote Warning: Cannot modify header information - headers already sent by (output started at ...:38 So my question is how can I output the array data into a string, so that I can print it out later. Like what am I doing wrong? Is there a better way to do this? Thanks! Hi, I've inherited a website with a problem! Basically the site runs pommo mailing list, which has worked fine for years. Recently though the website domain name changed. Since then - or at least at a similar time - pommo has just stopped working completely. It does nothing but outputs blank pages. I've never done any php development so I've no idea where to start in terms of debugging this. I've checked that the files etc all exist on the server: they do. I've uploaded a test html page which works. I've uploaded a test php page containing: <?php phpinfo( ); ?> and it works. But when I visit any page in the pommo application, the browser just renders a totally empty white page - no source HTML at all, just nothing! Can anyone suggest how to debug this? Even an error message would be somewhere to start. Thanks all! Hi guys, I need to pick the middle five numbers from a range of numbers example $lowest number = 0; // this will allways be 0 $highest_number = 10; // this can very from 1 to 10000 $selected_number = 5; // this can also very but it will allways be between 0 and highest number the middles numbers from the above 3 varuables should be 3 4 5 6 7 --- the $selected_number number allways needs to be the middle number // some of the issue we need to watch out for is, if hughest number is 4 then it should just go 1 2 3 4 Thank you guys Is this the correct syntax to check for an optional Middle Initial... Quote if (preg_match('/^?[A-Z]$/i', $_POST['middleInitial'])){ Thanks, Debbie I have a file named example.xml I want to know how to write to the 3rd line on this file? What function to use? I want to be able to write to this xml file right after the <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> tag. Please someone tell me how? Much appreciated. Code: [Select] <?xml version="1.0" encoding="UTF-8"?> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap> <loc>http://www.example.com/sitemap1.xml.gz</loc> <lastmod>2004-10-01T18:23:17+00:00</lastmod> </sitemap> <sitemap> <loc>http://www.example.com/sitemap2.xml.gz</loc> <lastmod>2005-01-01</lastmod> </sitemap> </sitemapindex> HELP!!! PLEASE!!! Here's what's happening: I have a string much like this one: Code: [Select] $str = "84,390961CPK_100,'Pink Coloured Cord',15,'390961CPK_100',1"; I need to insert apostrophes around the first instance of 390961CPK_100 so it reads '390961CPK_100' and not simply 390961CPK_100. To do this I have made the string into an array as follows: Code: [Select] $str_array = str_split($str); Now I need to insert quotes into the array of characters, then convert the whole mess into back into a string. I can't go by hard coded index numbers, because all of the text in this string is variable in length, so I have to go by the first and second positions of the commas. I have no idea how to do this, and it needs to be done yesterday! Mucho Gracias to anyone who can help me with this. I looked through all the array functions in the manual, but didn't see what I needed (might have just missed it) Is there a way to stick a value into the middle of an array, without writing over the current value? I would need it to just push the rest back 1. Hope that explains what Im looking for! Thanks in advance I am trying to figure out how to have an if statement in the middle of strings I am trying to concatenate. I have a from that has a address1 and address2, where address2 often does not exist. So I want to only add that line if there is actually content. I was trying something like the following, but that is giving me an error. I am pretty new to php, so I can't seem figure it out. Any help would be greatly appreciated. Code: [Select] "<p><strong>Address 1: </strong><br>" . $address1 . "<br>" if ( isset($address2) ) { echo . $address2 . "<br>" } . $city . ", " . $state . " " . $zip . "</p>" |