Jump to content

Lectrician

Members
  • Posts

    35
  • Joined

  • Last visited

Everything posted by Lectrician

  1. That's great, thank-you. I had kind of got there, but kept trying this and wondered why it would not work: <<INCORRECT CODE>> uasort($usersdata, function($1, $2) { return strcasecmp($1, $2); }); I also left my asort un-commented directy below the new code, so the new code was doing it's job, and then the asort was obliterating it's work! Twit.
  2. I have an array as below, and would like to sort it alphabetically by username. I know in the order shown below, asort works, but what if the username key is not the first? Any ideas on the best ways? Thanks :-) usersdata = array( [12345] = array( [username] => "Toby", => "toby@toby.com", [permissions] => "1,2,3,4" ) [12346] = array( [username] => "Tom", => "tom@tom.com", [permissions] => "5,6,7,8" ) [12347] = array( [username] => "Mable", => "mable@olddear.com", [permissions] => "1,2,3,4" ) [12348] = array( [username] => "Twit", => "twit@twit.com", [permissions] => "1,2,3,4,5,6,7" ) )
  3. Ok. I am asking some basic type questions I guess! When you open a file with a filehandle, is it safe to use the same file handle a couple times in a script, for different files, assuming one is closed before the other opened? I use $fh a lot in my scripts, and wonder if this is bad practice? I copy and paste code quite often to speed up my writing, and retain this filehandle usually.
  4. Hi, yes, PFsense is a firewall, and clients connect to it, receiving an IP by DHCP from it. PFsense is able to read the MAC and HOSTNAME, and use these to identify a user uniquely. Obviously they could be spoofed. I have not run into issues opening the files to search yet. They are not huge. No errors are reported. I was just wondering on the best method. Opening the file in chunks may be the best option. Thanks.
  5. So if I do: If(flock($file, LOCK_EX)){ Play with file } What happens if a file is already locked. Will it wait, or just pass this code completely?
  6. Thanks. Yes, the line for each user looks like this: timestamp | name | email | postcode | IP given | their MAC | hostname | HTTP header | a few other bits of info There can be 400 odd lines on busy days. I am running PFsense, and using the captive portal in it, altering the PHP to suit me. I check to make sure no cookie exists holding the name, email, postcode (these are auto-filled into the HTTP form fields when they go to connect). If no cookie exists, I open yesterdays logs, search each line for the users MAC address, and then auto fill. I think it is safe to assume the MACs wil be different, as this is how PFsense deals with identifying users. If yesterdays logs don't find a match, the script will go back 30 days. This whole searching the logs could be omitted, but it is preferred to have the form fields auto-filled, as customers are less annoyed! I am not sure I can setup a database on PFsense - I would need to have a look. Everything else on my version of PFsense runs as a flat file system. Newer versions do use an SQL database.
  7. Thanks. I am going to remove the IF statement to force all runs of the script to take action on the file. As it stands at the moment, if a lock is not obtained, the script does not take any action on the file. Thanks.
  8. Thanks. I was looking at two different scenarios. I have a log file which is appended and rotated daily. I can't alter how this written. Every night, my PHP script runs to extract data from each line. The files have become too large to open in one go into an array, so I altered the script to open the file and extract data line by line, using similar code to above(thanks). This now works fine. It got me to thinking. I have a log file which I append to, as users pass through a captive portal. It stores a users MAC, IP, HOSTNAME, HTTP HEADER, NAME, POSTCODE, EMAIL etc on a line. The file can be upto 400 lines long sometimes. When a user is presented with the captive portal, if they have passed through before, their data is auto filled. Their cookies are checked first, but if these have been cleared, up to 30 days of log files are opened, each line being checked for the users MAC. If a match is found, their details are auto filled. I can't decide if its best to open a whole log file into an array and work through each entry, or open the file line by line. This is where my speed vs memory query came from. I want it done quickly, but don't want to kill the process due to lack of memory (don't think it has ever died from lack of memory yet). Cheers.
  9. Hi. If I use Flock to lock a file once open, when other people run the script (visit webpage), will they queue in turn to access the file, eventually being able to access it? Is there a time out? I have been using an IF argument, so if no lock is made, the script continues. Looking at my code, this is undesirable. I would prefer the script to wait until the script can use the file. There are times when several people may be trying to access the same file - The script should run and complete in seconds. Thanks.
  10. Hi. I am working with some rather large files. Sometimes I am just opening a file and trying to find an entry on a single line. Sometimes I am opening a file and working on each line. Whats the most efficient way to work with a large file? If I open a file, and place all lines into an array, I will use a lot of memory. If I open a file, and work on it line by line, I should use much less memory, but will the process take longer? Whats most efficient? I have underlined my main question above. Thanks :-)
  11. Hi. I am trying to extract the main url from a string. For example. I want to extract: "http://google.com" from "http://google.com/search/pic.jpg" "https://secure.co.uk" from "https://secure.co.uk/secure/secure.gif" I am able to do this with this code, where $url is the url: preg_match("http://(.*?)/", $url, $matches); preg_match("https://(.*?)/", $url, $matches); Is there away to consolidate this into one regex search? Or am I being picky? Thanks!
  12. I have a script which runs to check TCP connections are open on a certain port. I had a need to check UDP connections, and so altered it as below. This works fine on my home Microsoft SBS IIS server, but timesout on my 1and1 hosting. function ping($host, $port, $protocol, $timeout) { if($protocol == "udp"){ $host = "udp://$host"; } $tB = microtime(true); $fP = fsockopen($host, $port, $errno, $errstr, $timeout); if(!$fP){ return "tcpdown"; } if($protocol == "udp"){ fwrite($fP, "\n"); $fread = fread($fP, 26); fclose($fP); if($fread == ""){ return "udpdown"; } } $tA = microtime(true); return round((($tA - $tB) * 1000), 0); } Any ideas why this timesout? Thanks.
  13. The input is something like this "6D5F470C7844490C785E4D48495F60494F585E454F454D422C" It is a couple of the boards systemwide variables cloaked into hex code. (that is hex code isn't it?). The code above should decloak this into binary which would reveal the systemwide variables.
  14. The below code is taken from my perl forums code. The code decloaks a formsession string sent as a hidden form element. It works fine to verify the form in Perl, but when I try to convert it to PHP I get errors conflicting outputs which are coming from the two lines below: $key = hex($hexkey); (PERL) $key = dechex($hexkey); (PHP) or $key = bin2hex($hexkey); (PHP) The Perl line above gives a different result to the PHP line(s). I have managed to convert down to this line from the top. Any help in figuring out why the perls HEX command varies from the PHP DECHEX command would be great! Also any pointers in converting from there downwards! (I know perls LENGTH is PHP STRLEN, and can sort the for loop). Thanks in advance for any help! $input = $_POST["formsession"]; if ($input !~ /\A[0-9A-F]+\Z/) {return $input; } # probably a non cloacked ID as it contains non hex code $input =~ s~0$~~; $hexkey = substr($input,length($input)-2,2); $key = hex($hexkey); for($n=0; $n < length($input)-2; $n += 2) { $dec = substr($input, $n, 2); $ascii = hex($dec) ^ $key; # xor it to reverse it $ascii = chr($ascii); $user .= $ascii; return $user;
  15. SORTED! After a bit more playing to see when the error occurs it was happening everytime the image width was above the value that the script decides to resize, so I looked at the resizing portion and found I had miss-typed a variable. A simple error! Amazing how it can stare you in the face!
  16. I guess. <?php $ThumbWidth = 250; $ImageWidth = 1000; $maxsize = 700000; $albumname = $_POST["albumname"]; $albumdescription = $_POST["albumdescription"]; $albumsecurity = $_POST["albumsecurity"]; $image1title = $_POST["image1title"]; $image1description = $_POST["image1description"]; $image2title = $_POST["image2title"]; $image2description = $_POST["image2description"]; $image3title = $_POST["image3title"]; $image3description = $_POST["image3description"]; $image4title = $_POST["image4title"]; $image4description = $_POST["image4description"]; $image5title = $_POST["image5title"]; $image5description = $_POST["image5description"]; $username = $_POST["username"]; $viewuser = $_POST["viewuser"]; $formsession = $_POST["formsession"]; if (empty($username) ){ $redirect = "Location: http://www.my_forum.co.uk/cgi-bin/yabb/YaBB.pl"; header( $redirect ) ; } $filename1 = time(); if (!file_exists("./images/$viewuser")){ mkdir("./images/$viewuser", 0755); mkdir("./images/$viewuser/thumbs", 0755); } $imagespath = "./images/$viewuser"; //Upload Images $fileloop = 1; $timestamp = time(); $filename = $timestamp; while ($fileloop <= 5){ if ($_FILES["image$fileloop"]["name"] != "") { if ($_FILES["image$fileloop"]["size"] > $maxsize) { $filestatus[$fileloop] = "filetoolarge"; } if ($_FILES["image$fileloop"]["error"] > 0) { $filestatus[$fileloop] = "failed"; } $filenameext = substr($_FILES["image$fileloop"]["name"], strrpos($_FILES["image$fileloop"]["name"], '.') + 1); $filenameextlower = strtolower($filenameext); if (($filenameextlower != "jpg") && ($filenameextlower != "jpeg") && ($filenameextlower != "png") && ($filenameextlower != "gif")) {$filestatus[$fileloop] = "incorrectfile";} if (($filestatus[$fileloop] != "failed") && ($filestatus[$fileloop] != "filetoolarge") && ($filestatus[$fileloop] != "incorrectfile")){ $fullfilename[$fileloop] = "$filename.$filenameextlower"; $target = "$imagespath/$fullfilename[$fileloop]"; $targetth = "$imagespath/thumbs/$fullfilename[$fileloop]"; $tmpfile = $_FILES["image$fileloop"]["tmp_name"]; $filetype = $_FILES["image$fileloop"]["type"]; if($filetype == "image/pjpeg" || $filetype == "image/jpeg"){ $new_img = imagecreatefromjpeg($tmpfile); }elseif($filetype == "image/x-png" || $filetype == "image/png"){ $new_img = imagecreatefrompng($tmpfile); }elseif($filetype == "image/gif"){ $new_img = imagecreatefromgif($tmpfile); } list($width, $height) = getimagesize($tmpfile); $imgratio=$width/$height; //Decide if to resize and save thumb if ($width > $ThumbWidth){ if ($imgratio>1){ $newthumbwidth = $ThumbWidth; $newthumbheight = $ThumbWidth/$imgratio; }else{ $newthumbheight = $ThumbWidth; $newthumbwidth = $ThumbWidth*$imgratio; } $resized_thumb = imagecreatetruecolor($newthumbwidth,$newthumbheight); imagecopyresized($resized_thumb, $new_img1, 0, 0, 0, 0, $newthumbwidth, $newthumbheight, $width, $height); if(ImageJpeg ($resized_thumb,$targetth)) {$thumbstatus = 1;} }else{ if(copy($tmpfile, $targetth)) {$thumbstatus = 1;} } //Decide if to resize and save image if ($width > $ImageWidth){ if ($imgratio>1){ $newimagewidth = $ImageWidth; $newimageheight = $ImageWidth/$imgratio; }else{ $newimageheight = $ImageWidth; $newimagewidth = $ImageWidth*$imgratio; } $resized_image = imagecreatetruecolor($newimagewidth,$newimageheight); imagecopyresized($resized_image, $new_img1, 0, 0, 0, 0, $newimagewidth, $newimageheight, $width, $height); if(ImageJpeg($resized_image,$target)) {$imagestatus = 1;} }else{ if(move_uploaded_file($tmpfile, $target)) {$imagestatus = 1;} } // if ($imagestatus != 1 || $thumbstatus != 1) {$filestatus[$fileloop] = "GDfailed";} } } if ($filestatus[$fileloop] == "GDfailed"){ $redirect = "Location: http://www.my_forum.co.uk/cgi-bin/yabb/YaBB.pl?action=viewgallery;username=$viewuser;phperror=2"; } if ($filestatus[$fileloop] == "filetoolarge"){ $redirect = "Location: http://www.my_forum.co.uk/cgi-bin/yabb/YaBB.pl?action=viewgallery;username=$viewuser;phperror=3"; } if ($filestatus[$fileloop] == "failed"){ $redirect = "Location: http://www.my_forum.co.uk/cgi-bin/yabb/YaBB.pl?action=viewgallery;username=$viewuser;phperror=4"; } if ($filestatus[$fileloop] == "incorrectfile"){ $redirect = "Location: http://www.my_forum.co.uk/cgi-bin/yabb/YaBB.pl?action=viewgallery;username=$viewuser;phperror=5"; } $fileloop++; $filename++; } /////end of upload $totalsize = ($_FILES["image1"]["size"] + $_FILES["image2"]["size"] + $_FILES["image3"]["size"] + $_FILES["image4"]["size"] + $_FILES["image5"]["size"]); if ($redirect == ""){ $Curl_Session = curl_init(); curl_setopt ($Curl_Session, CURLOPT_URL, "http://www.my_forum/cgi-bin/yabb/YaBB.pl?action=viewgallery;ref=addalbum2"); curl_setopt ($Curl_Session, CURLOPT_POST, 1); curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "username=$username&viewuser=$viewuser&formsession=$formsession&gallerysecurity=zpqm1000&albumname=$albumname&albumdescription=$albumdescription&albumsecurity=$albumsecurity&image1=$fullfilename[1]&image1title=$image1title&image1description=$image1description&image2=$fullfilename[2]&image2title=$image2title&image2description=$image2description&image3=$fullfilename[3]&image3title=$image3title&image3description=$image3description&image4=$fullfilename[4]&image4title=$image4title&image4description=$image4description&image5=$fullfilename[5]&image5title=$image5title&image5description=$image5description&totalsize=$totalsize&galid=$timestamp"); curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1); $result = curl_exec ($Curl_Session); curl_close ($Curl_Session); if ($result == 1){ $redirect = "Location: http://www.my_forum.co.uk/cgi-bin/yabb/YaBB.pl?action=viewgallery;username=$viewuser;album=$timestamp"; }else{ $redirect = "Location: http://www.my_forum.co.uk/cgi-bin/yabb/YaBB.pl?action=viewgallery;username=$viewuser;phperror=1"; } } header ( $redirect ); ?>
  17. Using GD to convert uploaded files into two seperate files of different size (one a thumbnail). Random images are saved as black. This may be just the thumbnail, just the larger image or both. It seems if an image does work successfully, it always will in the future, it just seems to be random images. I am using JPG - PNG - GIF, and have the same results with all. The file does exist and does have a size in KB. It seems by googling that this does happen, but I cannot find any help as to how to sort it out. Advice appreciated!
  18. I think the reason I have problems is because the forum sees the request coming in as from a new user (different computer [the server] with no cookie). So, is there a way to tell cURL to use the local users cookie rather than trying to use the servers one?
  19. I am using cURL to send some POST data from my PHP script into my PERL YaBB forum. The trouble is, when the POST data goes to the forum, the forum appears on the screen (CURL FOLLOW ME SET) but the forum shows as the user is logged out even though they are logged in with cookie set. The URL shown in the address bar is also the URL of the PHP script and not the forums form recieving URL. Any clickable buttons on the forum are also showing the PHP URL instead. How do I send the POST data and redirect to the correct URL too?? Pointers greatly recieved!
  20. Thanks. When the form is submitted a new page opens through my perl script. When you hit the back button to go back to this blank php page and then press refresh (because the page has expired), the session will have been cleared wont it? So when ever the php is executed the session will be renewed?
  21. If the page is refreshed though, the form data would be set ack into the session and so would exist? Or am I barking?
  22. Thanks. I have been googling, and it seems you can send some form POST data via headers? I have tried this code, but it returns a server error, with the log showing that the headers where malformed? Any ideas? <?php $host = "www.example.com"; $path = "/path/to/script.php"; $data = "data1=value1&data2=value2"; $data = urlencode($data); header("POST $path HTTP/1.1\r\n" ); header("Host: $host\r\n" ); header("Content-type: application/x-www-form-urlencoded\r\n" ); header("Content-length: " . strlen($data) . "\r\n" ); header("Connection: close\r\n\r\n" ); header($data); ?>
  23. I have a form submitted to a PHP script. Is there a way to use PHP to tell if the page which the form was submitted to is fresh from the form or has been refreshed by the user?
  24. One more thing..... How can I prevent the PHP scripft from re-running after a page refresh? My users fill ina form, the data is submitted to the PHP script, the PHP script plays with the data and then sends it back off to a Perl script (where it actually originated from). The auto submit of the form is doing this. once my Perl script has finished with the data from the PHP submitted, if a user presses the back button they get the blank PHP page. If they then refresh this page the data from the initial form is acted on twice (in my case, the same images are uploaded again, and given a different random filename). I would like to prevent this! New to PHP, so please be patient with me :-)
  25. Thanks - I figured it in the end! Thanks for the help.
×
×
  • 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.