Jump to content

davil

Members
  • Posts

    39
  • Joined

  • Last visited

Everything posted by davil

  1. eff it, for now I just replaced it with $errors++;TryAgain(); and removed the increment from the function. it makes no sense why it wouldn't work, but the site isn't mission critical so I don't need to spend too much time on it. Thanks anyway for all your help
  2. This is strange, seems it works fine as standalone PHP but when included into my project at large, it doesn't work as expected. I've checked and there are no other references to $errors, I even changed to $errorz, still no luck... I'll bet there's an easy fix for this but I'm lost for the moment, will keep trying..
  3. Thanks Keith. perhaps it is my server software (uniform server) - I will look into it. What version PHP and Apache you using ? if you're using Apache at all that is?
  4. Yes that's very true. here's the code in its entirety (I've moved the function to the very top of the code now outside the else statement but it's still not working) <?php$errors=0; // set at 0 at the very start {doesn't seem to help though}function TryAgain(){global $errors; //reference the global variable instead of just internal$errors++; //increment the $errors variable;echo '<br/><br/>Please try again <a href="index.php?locate=admin&sub=add_admin">here</a>';echo "<br/>errors=[$errors]";}?><h1>Add a new Administrator Account</h1><?phpif (!isset($_REQUEST['frubmit'])){//check who's logged in$currentuser=$_SESSION["login"];ECHO <<<HEREZ<br/>Here is a list of the current users:<br/><table class='userlist2'><tr>HEREZ;$sql="SELECT `login` FROM `users`";$result = mysql_query($sql) or die (mysql_error());while ($row = mysql_fetch_array($result)){foreach ($row as $key => $value){$$key = stripslashes($value);}echo "<td>$login</td>";}echo <<<HEREZ</tr></table><form enctype="multipart/form-data" method="post" action="index.php"><input type="hidden" name="locate" value="admin"><input type="hidden" name="sub" value="add_admin"><br/>Add a new username:<input type='text' name='newuser' /><input type='submit' name='frubmit' /></form>HEREZ;}else{//NEW USER NAME HAS BEEN SUBMITTED, CHECK IT OUT AND THEN ADD IF OKAY$newuser=$_REQUEST['newuser'];echo "<br/>Trying to add user: $newuser<br/><br/>";if ($newuser){}else{// field was left blank, report this to user and give them option to try againecho '<br/>Field was left empty, you must pick a username.';TryAgain();}if (strlen($newuser)<5){echo 'Username should be at least 5 characters.';TryAgain();}if (!$errors){echo "continue to SQL for adding a user [errors=$errors]";}}?> Basically I don't want it to continue to the SQL stuff (not written yet) unless $errors evaluates to false (or 0). I increment $errors in the TryAgain() function, but for some reason in the above code it doesn't stick. The way I check this is I'm echoing out the value of $errors within the function and outside the function. If you set $newuser to something like 'abc' it echoes out that it is too short, and echoes out $errors as 1 , but then outside the function it reverts back to 0
  5. also the code I posted above seems to work fine on its own but not within my program in an Else{ } block, this must be the key, I'll probably figure it out now any second.
  6. ah I have noticed that the way global variables worked in PHP4 has changed kinda since PHP5, as a security measure - is this right? Here's some info http://wiki.lunarpages.com/PHP_and_Register_Global_Variables unfortunately that applies to POST and GET stuff and I'm just wondering about within a function? should it not still work? I'm hesitant to change the setting in PHP.ini in case this is a seperate thing altogether.
  7. $errors=0; //NEW USER NAME HAS BEEN SUBMITTED, CHECK IT OUT AND THEN ADD IF OKAY $newuser=$_REQUEST['newuser']; function TryAgain(){ global $errors; $errors++; echo '<br/><br/>Please try again <a href="index.php?locate=admin&sub=add_admin">here</a>'; echo "<br/>errors=[$errors]"; } echo "<br/>Trying to add user: $newuser<br/><br/>"; if ($newuser){ }else{ // field was left blank, report this to user and give them option to try again echo '<br/>Field was left empty, you must pick a username.';TryAgain(); } if (strlen($newuser)<5){echo 'Username should be at least 5 characters.';TryAgain();} if (!$errors){echo "continue to SQL [errors=$errors]";} what is wrong with this code? For some reason if I increment $errors with $errors++ within the function it doesn't increment outside the function's scope. even though I declare it as a global at the start of the function. I know I'm doing something stupid here but can somebody please tell me what it is? Thanks
  8. Also I've just noticed that reading the file into a string and then changing it to an array was an unnecessary step - anybody wanting to do similar code to the above should use file() function rather than fread() and use the foreach function to step through the array rather than a 'for' up to the counted number of lines in the file - the script should be far more efficient that way. I was just throwing that one together and learned quite a bit about my bad coding practices so far.
  9. Perfect!! I new it would be something simple. Thanks very much ignace!!
  10. I used to always be able to write to windows text files and create newlines with a simple \r\n but now the text files are being written and \r\n appears in the textfile instead of a newline. I've also tried just \r or just \n but to no avail. I need to edit the services file on multiple XP machines here and add a line, but only if it hasn't been added before. My Code is far from finished but I want to be able to log the process to a text file. also the services file that I'm trying to append to is also adding the \r\n instead of a newline. Here's my code: <?php $windir=getenv('WINDIR'); $dcserver=getenv('LOGONSERVER'); $hostname=getenv('COMPUTERNAME'); $logfile="$dcserver\\NETLOGON\\sap_update_2010\\logs\\$hostname.txt"; $svcfile="$windir\\system32\drivers\\etc\\services"; $logfileHandle = fopen($logfile, 'w'); //OPEN LOG FILE FOR OUTPUT $inputfileHandle = fopen($svcfile, 'r'); //OPEN SERVICES FILE FOR READING (WILL WRITE TO THIS LATER) fwrite($logfileHandle, 'Log File Start... \r\n Test Line 2'); // MORE LOGGING TO COME LATER BUT FOR THE MOMENT I CAN'T GET NEWLINE TO WORK //LOAD IN SERVICES FILE FOR CHECKING $number_of_lines=count(file($svcfile)); //set a variable for amount of lines in services file if ($number_of_lines>10){ //If the file is normal length & no issues $data1 = fread($inputfileHandle, filesize($svcfile)); //read all the lines into a large variable fclose($inputfileHandle); //once read into an variable, we can now close the file handle $array1 = explode("\r\n", $data1); // Explode all Data into an array - line by line for ($a=0;$a<= $number_of_lines-1;$a++){ //step through line by line searching for the sapmsPR2 string if (strstr($array1[$a],'sapmsPR2')){ $done=1; } } if (!$done){ // If the line hasn't already been added, we need to add this line to the file $addline='\nsapmsPR2 3600/tcp #SAP PORT - ROLLED OUT BY SCRIPT'; // This is the line to be added to services file $outp1 = fopen($svcfile, 'a'); //OPEN SERVICES FILE FOR OUTPUT [append] fwrite($outp1,$addline); // WRITE THE LINE TO THE FILE [at the end] fclose($outp1); // CLOSE WHEN FINISHED //ALSO WE NEED TO COPY THE SAPLOGON.INI file to the machine if (!copy($inifile, $newinifile)) { fwrite($logfileHandle, 'Error - failed to copy SAPLOGON.INI across...\n'); }else{fwrite($logfileHandle, 'SAPLOGON.INI copied successfully to Windows folder...\n');} } } //Finish up by closing the log file fclose($logfileHandle); ?> Oh yes, I suppose an important thing to note here is that I'm trying to compile a simple windows CLI exe. to do this I've tried the following with no luck: bamcompile [http://www.bambalam.se/bamcompile/], PHC [http://wiki.swiftlytilting.com/Phc-win#Download] But I've a feeling it's not the compilers because even my WAMP setup is doing the same thing now, whereas before \r\n was fine. I know when reading in files and 'exploding' the data into an array - \r\n works fine, do I need to output to a file differently? Another stupid Question - Is it because I upgraded my own machine to Windows 7 since I last tried something like this - does Win7 treat files differently? I tried to figure out how to do this in binary but I've a feeling it might be easier than all that. If anybody can help me out I'd really appreciate it because I'm on a deadline and don't want to have to re-write this in nasty Autohotkey script or freebasic. and I'm no good at any other decent scripting languages. P.S. just in case anybody wonders, I've been running this EXE as an administrator on my win7 machine so as to give access to services file, but will wrap it in proper admin credentials later for running on the XP machines.
  11. In the end I figured out the way to set the proxy before using PHP's proper curl, and it worked. sorry. The following code reads the info into a variable , $x $ch = curl_init("http://support.dell.com/support/topics/global.aspx/support/my_systems_info/details?c=us&l=en&s=gen&ServiceTag=$serial&~tab=2&~wsf=tabs"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_PROXY, "http://10.10.10.10:80"); // Where 10.10.10.10 is the proxy IP and 80 is the port curl_setopt($ch, CURLOPT_PROXYPORT, 80); // Dunno why port 80 is set twice, but I copied this code and it works great curl_setopt ($ch, CURLOPT_PROXYUSERPWD, "myusername:mypassword"); $x = curl_exec($ch);
  12. Hi all, I'm having a problem retrieving some information from the Dell website. I want to write the PHP to go through a text file and grab down warranty info etc from the Dell website for each service tag, I can parse the list no problem but first I want to make sure I can get info down for just one machine, I can do it in my browser like so: IF YOU JUST WANT TO GET INFO FROM TAB1 [WARRANTY INFO], USE THIS (replace ABCDEFG with whatever service tag): http://support.dell.com/support/topics/global.aspx/support/my_systems_info/details?c=us&l=en&s=gen&ServiceTag=ABCDEFG&~wsf=tabs IF YOU JUST WANT TO GET INFO FROM TAB2 [ORIGINAL SYSTEM CONFIGURATION], USE THIS (replace ABCDEFG with whatever service tag): http://support.dell.com/support/topics/global.aspx/support/my_systems_info/details?c=us&l=en&s=gen&ServiceTag=ABCDEFG&~tab=2&~wsf=tabs but when I use curl.exe (I'm on Windows) it just gives me HTML of a page requesting the service tag again. As you will see in my code I tried spoofing my user agent, but with no luck. <?php // spoofing Useragent $useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"; $useragent="Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.9 (KHTML, like Gecko) Chrome/6.0.400.0 Safari/533.9"; $useragent="Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)"; //I've tried all of these strings, but to no avail $serial='abcdefg-replacewithyourownserialnumber'; $url="http://support.dell.com/support/topics/global.aspx/support/my_systems_info/details?c=us&l=en&s=gen&ServiceTag=$serial&~tab=2&~wsf=tabs"; $stringtosend="curl --user-agent \"$useragent\" -0 $url"; $txt=shell_exec($stringtosend); echo $txt; ?> also the reason I'm using curl.exe and not PHP's curl classes is because I'm behind a proxy and was able to use "proxifier" to re-route curl.exe requests but couldn't get PHP working the same way (tried php.exe , should I have tried apache.exe? ) anyway if anybody knows why the page would react differently to curl than it would in browser , any help is much appreciated. Thanks
  13. Yes you are right I haven't yet checked the link, but I did say I'd check out sockets (fsockopen) - I'm posting on my iPod last couple of days as I'm at an Arts festival, give a guy a chance lol. Thanks for ur help though, didn't mean to be rude bout the acronym thing it was just that I posted my train of thought rather than changing the post after I figured out the CLI thing
  14. Yeah I might just learn a bit bout sockets, I'm okay with Php but network protocols never were my forte
  15. I can ping them using "ping", I am unfamiliar with CLI. Hold on, A quick google tells me CLI means command-line interface. strange I never heard that acronym in 16 years of usings PCs lol but I presume you mean command prompt / terminal or whatever. anyway, to get back to the point, yep they'll ping no problem. I'm not sure if it's vlan1 (about 75% sure) or whatever but it's a fairly basic setup on most of the switches yes.
  16. Hi Andrew, thanks for the post, I know a little about the OSI model etc and I know about SNMP but the problem is I'm not the overall network administrator of the wider WAN we work on and the SNMP community settings aren't the same on every switch I deal with, therefore ping is my only option currently. I do know however that SNMP will give better information about the switch's health but I'm fighting a losing battle as I've no CCNA behind me and I've only been given limited access to the switches (show ver etc.) through my tacacs authentication. So as usual, I'm left doing things the awkward way. We had an old copy of solarwinds ipmonitor and it was very handy but the server died on it's ass. so I'm just trying to come up with a basic system that covers our needs, but I saw a tweet last night that linked me to this: http://www.softinventive.com/products/total-network-monitor/ might just try that as it's free. Getting money is difficult at the moment with the state my country is in.
  17. Hi all, with the help of some other person's code (not sure where I got it as it was a while back, but thanks to that person all the same), I've created a simple ping monitor in PHP that will ping all machines in a given INI file and then report back if any of them aren't working. it's very basic but it seems to work for most of our servers here but it can't monitor Cisco switches for some reason, probably to do with the protocol involved but I'm no networking expert so I wonder if anybody can help with this? <html> <head> <style type='text/css'> *{ font-family:verdana,tahoma,arial; font-size:17px; } .light{width:30px;} h1{ font-size:25px; } </style> <meta http-equiv="refresh" content="30"> </head> <body> <?php $time1=date('H:i:s'); echo "Last Refresh Time = $time1<br/><hr/>"; error_reporting(0); /*-----------------------------------------------------------------------------------------*/ // Checksum calculation function function icmpChecksum($data) { if (strlen($data)%2) $data .= "\x00"; $bit = unpack('n*', $data); $sum = array_sum($bit); while ($sum >> 16) $sum = ($sum >> 16) + ($sum & 0xffff); return pack('n*', ~$sum); } /*-----------------------------------------------------------------------------------------*/ function PingTry1($pingaddress){ // Making the package $type= "\x08"; $code= "\x00"; $checksum= "\x00\x00"; $identifier = "\x00\x00"; $seqNumber = "\x00\x00"; $data= "testing123"; $package = $type.$code.$checksum.$identifier.$seqNumber.$data; $checksum = icmpChecksum($package); // Calculate the checksum $package = $type.$code.$checksum.$identifier.$seqNumber.$data; // And off to the sockets $socket = socket_create(AF_INET, SOCK_RAW, 1); socket_set_option ( $socket, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>1, "usec"=>0) ); socket_connect($socket, $pingaddress, null); $startTime = microtime(true); socket_send($socket, $package, strLen($package), 0); if (socket_read($socket, 255)) { return true; } else{ return false; } socket_close($socket); } /*-----------------------------------------------------------------------------------------*/ function DoTheCheck($name,$ip){ global $errors; global $j; if (PingTry1($ip)==1){ //do nothing //ECHO " $name ....... --> UP<br/>"; }else{ $j++; $errors[$j] = "$name --> $ip"; } } /*-----------------------------------------------------------------------------------------*/ //READ IN THE INI FILE INTO $filedata Array $myFile1="hosts.ini"; $filehandle1 = fopen($myFile1, 'r') or die("Couldn't open file [$myFile1]"); $number1=count(file($myFile1));; $filedata = fread($filehandle1, filesize($myFile1)); fclose($filehandle1); // Create an array with each line of the file $array1 = explode("\r\n", $filedata); unset($filedata); //free up a bit of memory foreach ($array1 as &$line) { // step through the array, line by line if (!empty($line)){ list ($name,$ip)=split(",",$line); DoTheCheck($name,$ip); } } if ($errors){ echo 'The Following Hosts are down - <br/><br/><table>'; foreach ($errors as &$value) { $k++; echo '<tr><td><img class="light" src="red.png" /></td><td>'.$errors[$k].'</td></tr>'; } echo '</tr></table>'; } else{echo '<img class="light" src="green.png" /><h1>ALL IPS ARE UP!</h1>';} ?> </body> </html> here are the green.png and red.png files if anybody wants to try this out: just rename them to green.png and red.png and store in same folder as the PHP above. also you'll need to create a hosts.ini text file that just looks like this: Myserver1,10.100.0.1 My Other Server [the NAS],10.100.1.12 Myserver-3,10.124.0.1 you get the idea. Any help is much appreciated.
  18. Ok it seems to be working now (nearly finished). Previously I had to add this code to get IE6 to play ball: if (!XMLHttpRequest) { window.XMLHttpRequest = function() { return new ActiveXObject('Microsoft.XMLHTTP'); } } but since your code changes, I no longer need that piece of code (weird!) - anyhow, it's working all but for one small issue: depending on the timing of when you click upload, the window.location redirect might happen a second time, which plays havoc with my PHP code, which expects to be run once (it copies the original upload file elsewhere and deletes it at the very start, so when looped it can't find the file) I could do some sort of $_SESSION hack with a variable like 'stayput' or something, but there must be a better way to do this?? I'm so close now I can almost taste it. :-D
  19. Ah tis working! I have no way of testing in IE6 until tomorrow since I upgraded to win7 at home and I can't do XP mode because my CPU doesn't support. will test tomorrow though. I'm confident it should work.
  20. Ah it's still not budging :-( I must see if I can debug what's going on here [edit] ahah I see what I've done wrong now. filecheck.php isn't actually the real filename, I need to change that, d'oh!!! will re-test now[/edit]
  21. Ah! Great stuff. will test as soon as I get home. Thanks again.
  22. sorry I just tested that new code and now it doesn't work in newer browsers either (tested in Google Chrome) - so I'll be reverting to original for the moment. However, it's probably something I neglected to do, I didn't fully understand what you meant by "I didnt set the inner html of the div , that can be done by adding $('#Davfilecheck').html(data); to the function. " perhaps you could explain a bit further ? sorry....
  23. Thanks Icarus. I'll give that a go. I did almost get it working today by enabling XMLHTTPRequest properly before my code, but I'm aware that the way I was doing things is bad bad bad.... I'm not that hot at JS, PHP's my language... but I'll get there.
  24. I found a great client side image resize solution: http://www.resize-before-upload.com/ but the free version doesn't have form integration, so I copied some ajax code off the internet and integrated it into my PHP - so it just keeps checking for a specific file to be uploaded and then the PHP just echos 'success' and if the ajax sees that it redirects to another page to continue the process Here's the JS (Jquery obviously) $(document).ready(function() { //filecheck.php is called every 2 seconds to ask server if file has been uploaded yet var refreshId = setInterval(function() { $('#Davfilecheck').load('scripts/filecheck.php'); var fc1 = new String(); fc1 = document.getElementById('Davfilecheck').innerHTML; if (fc1 == "Success!"){ alert ("The photo has been successfully uploaded to the site. Click OK to enter details for photo"); window.location="index.php?locate=upload&filedone=yes"; } }, 2000); }); And here's the 'filecheck.php' code <?php if (file_exists('../secure/uploads/uploadfile')){echo "Success!";} ?> This system works great with most newer browsers but when the file is uploaded in IE6 the JS doesn't seem to notice. I will upload my main PHP script if necessary but I'm pretty sure this is the part that won't work in IE6. Is the only option to ask the users to upgrade browser or is there a hack for getting this to work in IE6 ? My PHP coding is a lot better than my JS coding. :-D
×
×
  • 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.