Jump to content

mpsn

Members
  • Posts

    264
  • Joined

  • Last visited

Everything posted by mpsn

  1. mpsn

    php cli help

    Is it possible to use both fread(STDIN,10) with $argv in php cli? function foo($a,$b); function foobar($c); let's say I have these functions(I'm aware it's function headers), so I wanto use fread(STDIN,10) to choose the function, and $argv to pass in arguments to the above functions. Can someone show me quickly how to approach this, i'd appreciate it.
  2. mpsn

    php cli help

    This is related I guess, why doesn't it exit while loop when i type 'q'? <?php $task = "f"; function showTasks()//h { echo "TASKS:\n"; echo "b - show breakfast menu;" echo "l - show lunch menu;" echo "q - quit: Exit program\n"; echo "h: to view this list of options\n\n\n"; } while($task != 'q') { echo "Please choose a task: "; $task = fread(STDIN,10); echo $task; echo "\n"; if( trim($task) == "h") showTasks(); //other tasks will call other functions, i do later } echo "Thanks for using our menu!"; ?> All help appreicate.
  3. Hi, I'm getting an infinite loop using PHP and CLI. <?php #!/usr/bin/php -q $userName = fread(STDIN,10); $password = fread(STDIN,10); $isAuthenticated = false; $realUserId = "aaa"; $realPassword = "pass"; while( $isAuthenticated!=true ) { echo "Please login to begin: \n"; echo "Enter username: ".$userName."\n"; echo "Enter password: ".$password; //now check to make sure is registerd user //$userId = $u->userLogin($userName, $password); if($userName != $realUserId && $password != $realPassword) echo "Invalid username or password. Please try again.\n"; else { echo " You are logged in! You can begin.\n"; $isAuthenticated = true; } } ?> All help appreciated.
  4. <?php $pathexpress = "email/message/to/toFirstName"; $childLabelPath = "email/message/to/toFirstName/#text"; $curChildLabelPath = rtrim($childLabelPath, "/#text"); echo $curChildLabelPath; ?> But why is ouput in browser screen: email/message/to/toFirstNam ANy help plez
  5. I didin't mention my problem in my previos post! In windows cmd, it outputs: cannot read input file:c:\dir\to\the\above\script
  6. I checked in windows cmd: php -v and outputs: PHP 5.3.5 (cli)... and this is little script I'm trying to run: <?php //TEST CLI php $quit = false; function greet($name) { echo "Hello, $name! \n"; } while($quit != true) { echo "Please enter your name: "; $input = fgets(STDIN); greet($input); echo "Do you want to be re-greeted?"; $quit = fgets(STDIN); } ?> Please any help would be great
  7. Hi, I need just quick run down on how to run a command line user interface for program for php using Windows command prompt: Let's say I have a calculator program: <?php class Calculator { function factorial($num) { if($num==1) return 1; else return $num*factorial($num-1); } //other functions... }//END CLASS Calculator ?> //Test runner: $calculator = new Calculator; $quit = false; while($quit != true) { echo "Please enter the number to find //how do i pass user's keyboard input to the function?? //so now pass user input to $calculator->factorial(//user input??); echo "Do you want to run program again?' //Again need to take in user input I feel it is similar to Java or C, but I just need some refresher, a little rustry, is it printf('%d', $quit) etc???
  8. It's ok, I think I got it. The XML file needs to have an attribute that references the schema file as such: (direct from w3schools) <?xml version="1.0"?> ======================== <note xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com note.xsd"> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> ========================= So it means I can change the xsi:schemaLocation to the directory where the schema lies as in: xsi:schemaLocation="C:\dir\dir2\ note.xsd" or is that just a namespace so and the XML file will assume the schema is in same directory? Please any help!
  9. Hi, here is my validateXML function, it checks first if an XML file is well-formed and result $result is true, then if well-formed and schema provided, it does further check to see if schema validated and result $result returns true too (using php core function: schemaValidate(string $schemaFileName), so my let's say I use stuff.xml and houses.xsd, so even if stuff.xml is well formed, and the function goes to evaluate houses.xsd(assume also validated), then the $result of true doesn't really indicate that the well formed stuff.xml is also schema validated since schemaValidate only checks if a schema itself is validated, BUT how does it link the validated schema to the well-formed stuff.xml. In short, I am saying I can pass a wellformed xml file that with a validated schema file BUT the two may be totally different. Here is the function: </php public function validateXML($xmlFilename, $xmlSchema=null) { $result = false; //which line you don't understand $dom = new DOMDocument(); if ( $dom->load($xmlFilename) || $dom->loadXML($xmlFilename))//so this tests if it is well formed?yes { //and if false, $result is false //the xml is well-form, now test schema $result = true; if ( $xmlSchema ) // if we don't pass schema , mean we don't need to test the shcme, the validate will return true still { $result = $dom->schemaValidate($xmlSchema);//returns true on success } } //error occurrs, if there is not erro, this code will not run, because errors is empty array $errors = libxml_get_errors(); //stores each line as array elem foreach ($errors as $error) { print $this->showLibXMLErrors($error); } libxml_clear_errors(); return $result;//1 is TRUE, 0 is FALSE } ?> I'd appreciate any help!
  10. Hi, I have a mistmatched tag <messagesss></message> BUT it still displays "Validated XML!" BUT then proceeds to the else that outputs each XML error! here is the XML: <?xml version="1.0" encoding="utf-8"?> <email> <messagesss> <to> <toFirstName>Tove</toFirstName> <toLastName toType="common" style="swag">Smith</toLastName> </to> <from><fromdd/> <fromFirstName>Jani</fromFirstName> <fromLastName fromType="unique">Dravison</fromLastName> </from> </message> </email> <?php $dom=new DOMDocument(); $dom->load("emailSimple.xml"); $isValidated=false; $dom->formatOutput = true; $dom->saveXML(); $errors=libxml_get_errors();//Returns array where each XML file line is an elem if(!file_exists("emailSimple.xml")) print "no such file!"; else if(strlen(file_get_contents("emailSimple.xml"))==0) print "File is empty!"; else if($dom) {//IF file exists and has content if(empty($errors)) print "Validated XML!";//isValidated=true so now shred! else { //CHECK if current XML file is Well-formed foreach($errors AS $error) {//FOR EACH ERROR OF CURRENT XML FILE TO CHECK echo "Error Code: ".$error->code."<br />"; echo "Error message: ".$error->message; //Column is the end of the line where error is echo "line".$error->line.", column".$error->column."<br />"; echo "----------------------------------------------<br /><br />"; } } libxml_clear_errors(); } ?>
  11. Hi, I have a class XMLtool where one of the functions is to output a parameter-passed dom document and then output to the browser screen, so basically: function outputXML($domDocument) { if has attributes print cur nodes attributes for each child, if child is element node outputXML(cur node) else if child is text print text end for each } Please I am not sure what type of unit tests I can perform. I'd appreciate all the help!
  12. Hi, I want to build a simple calculator class and one of the function is to calculate the factorial, but for some reason I cannot get this to work. Here is class: ========= <?php class Calculator { function factorial($n) { if($n==1) return 1; else return factorial($n-1)*$n; } }//END CLASS Calculator $calculator=new Calculator(); print $calculator->factorial(3); ?> But it doesn't work, I know this is simple but I am a little rusty with object oriented programming. I'd appreciated any help!
  13. mpsn

    Login help

    No, it's actually not working. I don't understand. Please take a look, I appreciate any help!
  14. mpsn

    Login help

    Hi, actually I forgot to put session_start as first item in script so now it works new problem now (minor one also): when user just press Login, it goes to the Index.php RATHER than display the appropriate message: "Please fill in username and password! etc."
  15. mpsn

    Login help

    Hi, I don't know now why the login doesn't work. Here are the issues: on login script: ========== Issue 1)when I enter the correct login info, it still takes to unaunthenicated (no session set) index.php Issue 2)I noticed that the only way for it to display the missing fields message(eg: "Please fill in username!") when Login submitted is if I don't put in anything in <form action=""> rather than put in <form action="index.php'> here is login script: ============= <html> <head> <title>Login</title> </head> <body> <form method="post" action=""><!-WHY doesn't it show the missing fields conditions at buttom if I put in action="index.php"??--> <p> <label>Username:</label> <input type="text" name="username"> </p> <p> <label>Password:</label> <input type="password" name="password"> </p> <p> <input type="submit" value="Login" name="Login" /> <input type="reset" value="Reset" name="Reset" /> </p> </form> <?php //if username/password filled in and submitted, check db to find match login info if(array_key_exists("Login",$_POST) && !empty($_POST["username"]) && !empty($_POST["password"])) { $server="localhost"; $user="root"; $pass=""; $db="somedb"; $attemptedUsername=$_POST["username"]; $attemptedPassword=sha1($_POST["password"]); $dbObj=new mysqli($server,$user,$pass,$db); $SQL="SELECT userName,userPassword FROM users WHERE userName='$attemptedUsername' AND userPassword='$attemptedPassword'"; $getLoginInfoQuery=$dbObj->query($SQL); if($getLoginInfoQuery->num_rows==1) { session_start();//NB: Start session BEFORE doing any session stuff! $_SESSION["isAuthenticated"]="userAuthenticated"; } else//"Please register above!" print "Please register above!"; } if(array_key_exists("Login",$_POST) ) { if(empty($_POST["username"]) && empty($_POST["password"])) print "Please fill in a username and password! <br />"; else if(empty($_POST["password"])) print "<Please fill in a password!<br />"; else if(empty($_POST["username"])) print "Please fill in a username! <br />"; } ?> </body> </html> here is index.php: ============= <?php session_start(); if(isset($_SESSION["isAuthenticated"])) { require("disconnect.inc.php"); $username=$_POST["username"];//FROM login.php //print $username."<br />"; print ' <html> <head><title>Home</title></head> <body> <form id="logoutForm" name="logoutForm" method="post" action=""> <input name="logout" type="button" id="logout" value="Log out" /> </form> <hr />'; print "<p>WELCOME, you are accessing secret info! </p>"; } else { print "You must be registered or logged in to continue."; print "<hr />"; print "<a href='xmlShredderRegister.php'>Create account</a> <br />"; print "<a href='xmlShredderLogin.php'>Login</a>"; print "</body>"; print "</html>"; } ?> finally is here disconnect session script: (disconnect.inc): ======================================== <?php if(isset($_POST["logout"])) { $_SESSION=array(); session_destroy(); //redirect user back to login page header("Location: login.php"); exit; } ?> Please I'd appreciate any help as I've been going nuts about what should be pretty simple to resolve!
  16. Hi, I cannot find where to add foreign key constraints in phpmyadmin! Any help is appreciated!
  17. Here is above code, more readable: <?php $numActualUploads=chop($_POST["numUploadsHidden"], "/");//chop removes char to right of first param string if(isset($_POST["numUploadsHidden"])) {//LATER use:$_SESSION['..']=$_POST[] $xmlList=array();//STORES current XML files (USED TO PREVENT INSERTING SAME FILENAME TO DB for($i=0;$i<$numActualUploads;$i++) {//FOR EACH XSD:XML PAIR, DO: //CHECK EACH XSD:XML upload pair $curXSDFileName=$_FILES["uploadedXSDFile"]["name"][$i]; $curXMLFileName=$_FILES["uploadedXMLFile"]["name"][$i]; $curXSDFileExt=pathinfo($curXSDFileName,PATHINFO_EXTENSION); $curXMLFileExt=pathinfo($curXMLFileName,PATHINFO_EXTENSION); if(!empty($curXSDFileName) && !empty($curXMLFileName) ) { if($curXSDFileExt==".xsd" && $curXMLFileExt==".xml") { //IF xml file NOT in DB(check if on xmlList), foreach($xmlList AS $xmlItem) { if($xmlItem!=$curXMLFileName) { print $curXSDFileName.":".$curXMLFileName."<br />"; $xmlList[]=$curXMLFileName; } } } }//END BIG IF }//END FOR LOOP check each XSD:XML pairs }//END BIG IF ?>
  18. Wow, that is what I call a answer! I did it this way: here is pseudocode: ============== make a xmlList to store only unique xmlFile from ($_FILES) while still XSD:XML pairs to traverse, do: if the ith xsdFile and ith xmlFile not empty, then: if the ith xsdFile is .xsd AND the ith xmlFile is .xml, then: foreach item in xmlList, do: if current xmlList item!=ith xmlFile, then: store the ith xmlFile's filename to db store ith xmlFile to xmlList END IF END FOREACH END IF END IF END WHILE Here is code for 3rd script(upload.php), I have a hidden input type btw that stores the $_POST['numUploadsHidden'] (on the 2nd script: uploadform.php) from the first script where user types into textbox for total XSD:XML pairs BUT I'm not sure why it doesn't run, it DOES enter the BIG IF, but nothing else inside! $numActualUploads=chop($_POST["numUploadsHidden"], "/");//chop removes char to right of first param string if(isset($_POST["numUploadsHidden"])) {//LATER use:$_SESSION['..']=$_POST[] $xmlList=array();//STORES current XML files (USED TO PREVENT INSERTING SAME FILENAME TO DB for($i=0;$i<$numActualUploads;$i++) {//FOR EACH XSD:XML PAIR, DO: //CHECK EACH XSD:XML upload pair $curXSDFileName=$_FILES["uploadedXSDFile"]["name"][$i]; $curXMLFileName=$_FILES["uploadedXMLFile"]["name"][$i]; $curXSDFileExt=pathinfo($curXSDFileName,PATHINFO_EXTENSION); $curXMLFileExt=pathinfo($curXMLFileName,PATHINFO_EXTENSION); if(!empty($curXSDFileName) && !empty($curXMLFileName) ) { if($curXSDFileExt==".xsd" && $curXMLFileExt==".xml") { //IF xml file NOT in DB(check if on xmlList), foreach($xmlList AS $xmlItem) { if($xmlItem!=$curXMLFileName) { print $curXSDFileName.":".$curXMLFileName."<br />"; $xmlList[]=$curXMLFileName; } } } }//END BIG IF }//END FOR LOOP check each XSD:XML pairs }//END BIG IF **NOTE: I'm just printing it out at the moment for debugging! If you can take a look, thanks.
  19. Appreciate the constant as second parameter, but what do you mean it is a "tad faster because it uses internal engine", isnt' even php core string manipulation function also part of the internal engine, are you talking about the Zend engine?
  20. Thanks, I also found this useful tidbit: $dir=pathinfo("C:\dir\dir2\hello.xml"); print $dir["extension"];
  21. Hi, how do I extract just file extension? eg: $file="hello.xml"; $fileExt=??? print $fileExt; Any help much appreciated!
  22. mpsn

    Login help

    lol, that is hilarious! You're right! But I was just curious about sessions I guess, but that makes sense with browsers storing a cache so that's why the back buttons is not considered remembering current activity by user!
  23. Hi, I want to check conditions for what is being uploaded. Basically I want to allow users to upload many XSD schema and XML file, so each complete upload is when user gives an 1 XSD file for 1 XML file to validate against, then the successful files are uploaded to db. first page:(numUpload.html) ======= <input type="text" name="numUploads"> to ask how many upload XSD:XML pairs they want, press SUBMIT goes to uploadForm.php eg: if user types 2, then we have four upload forms (upload form for 1st XSD, upload form for 1st XML file; upload form for 2nd XSD file, upload form for 2nd XML file) second page(uploadForm.php): ===================== for loop to create each uploaded pair then the validated XML (using the XSD of course) have their filenames inserted into db press SUBMIT (goes to upload.php to validate each before entering the validated XML filenames to db) You see, I want to have it organized where first upload form for first XSD:XML pair must be .xsd and second form of pair must be .xml In short, this is what I need help to figure out how to code (the conditions to check before uploading): ========================================== *1)for a given upload form pair, the first upload form only accepts .xsd, the second form only accepts .xml 2)no duplicate XML files allowed (XSD is ok) 3)if user uploads an .xsd, there must be an .xml *eg for point 1)XSD#1[_________] (Browse...) XML#1[_________] (Browse...) ------------------------------------------ XSD#2 [_________] (Browse...) XML#2 [_________] (Browse...) Above is the upload forms I "drew", so I want to keep each pair as such where the first upload form for a given pair can only accept .xsd files and second form of a given pair of upload forms only accepts .xml files AND NO duplicate .xml files at all! Any help much appreciated!
  24. mpsn

    Login help

    If if want to keep track of last session(meaning whenever user refreshes the browser or clicks on a link/button to update the session information, will the session only keep track per browser? I noticed on this very excellent site www.phpfreaks.com whenever you open new windows for the browser you originally logged on in that each new window of same browser still keeps you logged and only updates the last activity time whenever browser is refreshed or clicks a link, BUT NOT update when user just clicks the back button of browser. But when I open a DIFFERENT browser, it shows I am logged out, so in short I just want to know is a session unique to each browser or you have to set it to remember if user is jumping b/t browsers for a given authenticatd site?
  25. How do I keep track of the checked checkboxes (I know I have to: <input type="checkbox" name="items[]" /> BUT if I use $_POST, how do I refer to each checked item? Any help much appreciated!
×
×
  • 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.