PHP - Do I Make This Neater? Or, Any Better Way For This If Block.
I'm trying to clean up some past coding for a project of mine and this block keeps standing out like a sore thumb, but it works and i can understand it.
What it's doing is comparing each of the users submitted data to the admin-set data and then flagging if there any errors or not. Any suggestions on a different approach? Code: [Select] foreach ($_SESSION['usersData'] as $k => $v) { foreach ($this->info as $m => $n) { if ($m == $k) { if ( ($n['required'] && ( empty($v) || !isset($v) ) ) ) { $_SESSION['errors'][$k] = TRUE; } elseif ( (!$this->escape_data($v,$n['type'])) && ($n['required']) ){ $_SESSION['errors'][$k] = true; } elseif ($n['required'] && ($v == $n['default'])) { if ($n['readonly'] != true) $_SESSION['errors'][$k] = true; } elseif ($n['required'] && $n['readonly'] && ($v != $n['default']) ) { # Box is required, set to readonly and has a default value, if this triggers then the user has tampered with our form. $_SESSION['usersData'][$k] = $n['default']; } elseif (isset($_SESSION['errors'][$k])) { unset($_SESSION['errors'][$k]);# = false; } } elseif ( isset($this->info['file']) ) { if ($this->info['file']['required'] && $_FILES['file']['size'] == 0) { $_SESSION['errors']['file'] = TRUE; } else { unset($_SESSION['errors']['file']); } } } } Similar TutorialsHello, Thanks to my new found knowledge of forms, I was able to make the following application! Let me just tell you a bit about what it does. I basically made this to calculate the percentage of the total scores from League of Legends, a game that me and some of my friends play. The game is played with 5 Players per team, and they can each Kill, Die, or Assist people throughout the game. The purpose of this application is to find out the % Kill, % Death and % Assist per Player in a team throughout one game. It calculates this by doing something like a Player1Kills/TotalKills*100 calculation. Here is the code which I wrote : Code: [Select] <!-- Author : Soufin 'FlameDra' Rahimeen Date Started : 4/1/2011 --> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>League of Legends - Calculator</title> </head> <body> <form action="" method="POST"> Player 1 : <input type="text" name="player1" value="Player Name"></input> K : <input type="text" name="player1K" value="Kills"></input> D : <input type="text" name="player1D" value="Deaths"></input> A : <input type="text" name="player1A" value="Assists"></input><br /> Player 2 : <input type="text" name="player2" value="Player Name"></input> K : <input type="text" name="player2K" value="Kills"></input> D : <input type="text" name="player2D" value="Deaths"></input> A : <input type="text" name="player2A" value="Assists"></input><br /> Player 3 : <input type="text" name="player3" value="Player Name"></input> K : <input type="text" name="player3K" value="Kills"></input> D : <input type="text" name="player3D" value="Deaths"></input> A : <input type="text" name="player3A" value="Assists"></input><br /> Player 4 : <input type="text" name="player4" value="Player Name"></input> K : <input type="text" name="player4K" value="Kills"></input> D : <input type="text" name="player4D" value="Deaths"></input> A : <input type="text" name="player4A" value="Assists"></input><br /> Player 5 : <input type="text" name="player5" value="Player Name"></input> K : <input type="text" name="player5K" value="Kills"></input> D : <input type="text" name="player5D" value="Deaths"></input> A : <input type="text" name="player5A" value="Assists"></input><br /> <br /> <input type="Submit" value="Submit"></input> <br /> <br /> </form> <?php $totalK = $_POST['player1K'] + $_POST['player2K'] + $_POST['player3K'] + $_POST['player4K'] + $_POST['player5K']; $totalD = $_POST['player1D'] + $_POST['player2D'] + $_POST['player3D'] + $_POST['player4D'] + $_POST['player5D']; $totalA = $_POST['player1A'] + $_POST['player2A'] + $_POST['player3A'] + $_POST['player4A'] + $_POST['player5A']; ?> <br /> <!-- Player 1 Area --> Player <?php echo $_POST['player1']?> has <?php $player1Kpercent = $_POST['player1K']/$totalK*100; echo round("$player1Kpercent") . "%"; ?> of total kills. He has <?php $player1Dpercent = $_POST['player1D']/$totalD*100; echo round("$player1Dpercent") . "%"; ?> of total deaths, and he has <?php $player1Apercent = $_POST['player1A']/$totalA*100; echo round("$player1Apercent") . "%"; ?> of total Assists. <br /> <!-- Player 2 Area --> Player <?php echo $_POST['player2']?> has <?php $player2Kpercent = $_POST['player2K']/$totalK*100; echo round("$player2Kpercent") . "%"; ?> of total kills. He has <?php $player2Dpercent = $_POST['player2D']/$totalD*100; echo round("$player2Dpercent") . "%"; ?> of total deaths, and he has <?php $player2Apercent = $_POST['player2A']/$totalA*100; echo round("$player2Apercent") . "%"; ?> of total Assists. <br /> <!-- Player 3 Area --> Player <?php echo $_POST['player3']?> has <?php $player3Kpercent = $_POST['player3K']/$totalK*100; echo round("$player3Kpercent") . "%"; ?> of total kills. He has <?php $player3Dpercent = $_POST['player3D']/$totalD*100; echo round("$player3Dpercent") . "%"; ?> of total deaths, and he has <?php $player3Apercent = $_POST['player3A']/$totalA*100; echo round("$player3Apercent") . "%"; ?> of total Assists. <br /> <!-- Player 4 Area --> Player <?php echo $_POST['player4']?> has <?php $player4Kpercent = $_POST['player4K']/$totalK*100; echo round("$player4Kpercent") . "%"; ?> of total kills. He has <?php $player4Dpercent = $_POST['player4D']/$totalD*100; echo round("$player4Dpercent") . "%"; ?> of total deaths, and he has <?php $player4Apercent = $_POST['player4A']/$totalA*100; echo round("$player4Apercent") . "%"; ?> of total Assists. <br /> <!-- Player 5 Area --> Player <?php echo $_POST['player5']?> has <?php $player5Kpercent = $_POST['player5K']/$totalK*100; echo round("$player5Kpercent") . "%"; ?> of total kills. He has <?php $player5Dpercent = $_POST['player5D']/$totalD*100; echo round("$player5Dpercent") . "%"; ?> of total deaths, and he has <?php $player5Apercent = $_POST['player5A']/$totalA*100; echo round("$player5Apercent") . "%"; ?> of total Assists. <b /> </body> </html> Just for example, some screenshots. For values input like : The result is : Now, as you can see the code is quite long and may be hard to understand in case I want to expand it in the future. So, I was wondering how I can use functions to make the code more readable and shorter. I'm sorry, I'm not used to using functions (yet). Thanks in advance! Hi all, I have a function that is only used in the file it is declared in : /index.php Code: [Select] /** * This function is used to display form messages. * * @param string $msg This explains login and session status . * * @access private * * @return string */ function pred($msg) { return "<p class='content' style='color:red;'> $msg</p>"; } I'm using netbeans 7.0 (which by the way rocks !!!!! ), and I don't want to be able to see the documentation in other files when I start typing something similar. I read at pear.net coding standards that the '@param private' can be used on class methods. Is there a way to make the documentation private to the file that the function is declared in? I'm simply trying not to flood other files in the project with needless function suggestions. Thanks in advance. Okay so my news script is set to view only 10 pieces of news. But I want it so that it starts a new page once I have more than 10 pieces of news. Code: [Select] <?php require("functions.php"); include("dbconnect.php"); session_start(); head1(); body1(); new_temp(); sotw(); navbar(); $start = 0; $display = 10; $query = "SELECT * FROM news ORDER BY id DESC LIMIT $start, $display"; $result = mysql_query( $query ); if ($result) { while( $row = @mysql_fetch_array( $result, MYSQL_ASSOC ) ) { news_box( $row['news'], $row['title'], $row['user'], $row['date'], $row['id'] ); } mysql_free_result($result); } else { news_box( 'Could not retrieve news entries!', 'Error', 'Error', 'Error'); } footer(); mysql_close($link); ?> I tried a few things but they failed....miserably. I have been reading most spambots use UTC-12 timezone and PHbb has a hack to block UTC-12, but I dont have the file I am looking to get the UTC ( or GMT ) time from the referring page any ideas? how to block someone from visiting my page using ip adress ? this is the case if someone come to my site then we record the ip address and put it into database. when the visitor come again with the same IP they cannot see my page (block them) and redirect it to somewhere page. I draw contents from a database. Some of the texts contain a footnote, which is formatted using a div class. Following the HTML of this:
<div class=""footnotes""> <br> <hr align=""left"" noshade=""noshade"" size=""1"" width=""150"" /> <blockquote> <a href=""#f1"" name=""fn1""> <span class=""superscript""> * </span> </a>Some footnotetext</blockquote></div>Since my bibliography uses data from the database as well, the footnote now appears before the references, but I would like it at the very bottom of the page. I was thinking of using preg_replace in order to separate the textfield into two variables, one for the text itself, the other one for the footnote (it is always just one) and integrate after the bibliography is compiled. Unfortunately, it seems that the preg_replace does not work. It always displays the whole content of the textfield. Here's the PHP: $text = preg_replace('/(.*)(\div class=\"footnotes\"\>.*?\<\/div\>)/s', '$1', $result['text']);$footnote = preg_replace('/(.*)(\div class=\"footnotes\"\>.+?\<\/div\>)/s', '$2', $result['text']); echo '<div align="justify"><span style="font-family:Georgia;font-size:16px;">' . $text; ***BIBLIOGRAPHY*** ... echo $footnote;Maybe someone has an idea how to deal with that. I tried it on phpliveregex. There search string works fine. Many thanks for any help. How is it that a site can forward to my site without a trace? www.2mysite.com is forwarding traffic to www.mysite.com but $page_name = $_SERVER['HTTP_REFERER']; echo $page_name; does not show www.2mysite.com as the referring site...how does one hide this? Help? It's one of those days after a long weekend and a rainy morning. Simply trying to get the correct message depending on the variable's value if($a = 'one'){ echo "POOR";} if($a = 'two'){ echo "GOOD";} if($a = 'three'){ echo "VERY GOOD";} if($a = 'four'){ echo "EXCELLENT";} Not sure if I need to use ==, extra quotes, or ELSEIF for the best result.
Hello, Basically, What I'm looking for would be for a method of blocking certain email addresses from being submitted in a form, I need it to block certain emails that are on the list. I think the best way to describe it would be a form submission blacklist that is checked before it gets submitted. Many thanks I had a website running a XML script which worked perfectly but I have updated it by using XMLReader and PHP 5. However, there is a security update on PHP 5 which prevents me from accessing files on my server. Someone has suggested I add a php.ini file with the following: allow_url_fopen = on allow_url_include = on However I have also read that the whole purpose of the security update is to prevent this. Can someone advise me how I can get around this issue so that my server can read files and still operate by the PHP5 security update. Any help would be greatly appreciated. Say I have a server written in PHP (don't ask ). Let's say I have 10 users connected to my script and one of those users, for the sake of the example, needs 10MB of data sent to them. If I send that data via socket_write(), does my program execution stop (therefor locking up every other user) until the data has gone through, or is the actual task of sending data over the tubes passed on to some lower level OS function so my program can get on with its flow? I have the code below working perfectly, but I was wondering if there is a more eloquent way to write this, specifically handling the exceptions... This creates an object of a Router Class that parses my clean URLs into a Controller name, method name and the rest are arguments. So for example: www.domain.com/users/view/profile/bobsmith would route to the Users Controller, and find the viewUsers method and pass the arguments "profile" and "bobsmith" to it, and the method would take it from there... works great. That explains what this code does. My only question is if there is a cleaner way to handle the exceptions, so I don't have "throw new exception" written four times. Thoughts? Or is it good to go? $rawURL = getPageURL(); $router = new Router($rawURL,$rootDIR); $controller = $router->getController(); $method = $router->getMethod(); $arguments = $router->getArguments(); try { // check controller exists and set controller path // if ((isset($controller) && ($controller != ''))) { $controllerPath = APPLICATION_PATH . "controllers/" . $controller . ".class.php"; // check controller path exists // if (file_exists($controllerPath)) { // set method name and check method exists // if ((isset($method) && ($method != ''))) { $methodName = $method . ucfirst($controller); if (method_exists($controller, $methodName)) { // test it out // $test = new $controller(); $response = $test->$methodName($arguments[0]); echo $response; } else { // method_exists failed // throw new Exception("That method does not exist foo!"); } } else { // method was not found in URL // throw new Exception("No method name was given foo!"); } } else { // file_exists failed // throw new Exception("That controller does not exist foo!"); } } else { // controller was not found in URL // throw new Exception("No controller name was given foo!"); } } catch (Exception $e) { // spit it out foo // echo $e->getMessage(); } Guys/Gals; This below block outputs a very simple progress bar, i'm curious if anyone would see a solution to perform a task when this bar reaches the end as you can see it's set to countinue until width<400 when that 400 is true I and in essence the process is done (even though nothing in reality) i want to display countinue but only when it's done echo "<table width='100%' border='1' cellpadding='1' cellspacing='1' bgcolor='#666666'><tr><td width='506' height='52' valign='top'><script type='text/javascript'>function progress(){if(document.images['bar'].width<400){document.images['bar'].width+=5;document.images['bar'].height=5; }else{clearInterval(ID);}}var ID;window.onload=function(){ID=setInterval('progress();',$cpuspeed);}</script><img src='images/white.gif' name='bar'/><br /><font color='#ffffff'>Cracking....</font></td></tr></table>"; any dideas Hello, I'm currently using this to block certain emails: $blacklisted = Array("email@email.com", "email2@email.com", "email3@email.com"); in with: if( !in_array( strtolower($email), $blacklisted ) ) { Ok, What I want to do, is be able to specify a domain extension such as '*@email.com' So anything with 'something'@email.com is blocked. I still wish to keep manual email address's in place aswell. Many thanks James Can this be done easily enough with php? I've attached my current code below, but don't see how i could add this in. Code: [Select] $name = $_FILES['file']['name']; $temp = $_FILES['file']['tmp_name']; $size = $_FILES['file']['size']; $random = md5(uniqid(rand(), true)); $random = substr($random, 0, 20); if (!$name || !$temp || !$size) { echo "Go back and select a file."; exit(); } foreach ($_FILES as $file) { if ($file['tmp_name'] != null) { $thisext1=explode(".", strtolower($file['name'])); $thisext=$thisext1[count($thisext1)-1]; if (!in_array($thisext, $extensions)) { echo "That file type is not allowed."; exit(); } } } Hey Guys, Hope you are all having a great day I was hoping somebody could help me with preventing my blog from being attacked by SQL Injection. I made a simple blog in using PHP and MySQL but I keep getting spam comments (even though I use re-captcha) and some files were overwritten on my web server. For all my input I use mysql_real_escape_string but I still get the problem. I found a video on youtube that showed how to enter stuff on the address bar like "order by 2--" and "union all select...." after passing a variable etc, and all of the things in the video could be replicated on my site I am guessing that is my problem, but the video did not tell me how to resolve the issue and I am sick of having to delete hundreds of spam comments per day and check my web server for uploaded files. How can I stop people adding these commands to the address bar and getting data from my database? I really need your help Thanx, Jen Hi, I want that user can only read my article but can't download it. neither with save page as or with view source, even not with selecting text. and copy and paste. is there any solution in php to do that. Please help Thanks Hi the I have a PDF form that generates an XFDF file (form field valies in XML form) and I am passing it to PHP for processing. I am having trouble pulling a specific value from the XML block because of the way that the XML data is generated and organized. Please see a sample of the XML block below: 'test.xml' file: Code: [Select] <?xml version="1.0" encoding="UTF-8"?> <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve"> <annots/><fields> <field name="Non"> <value>Off</value> </field> <field name="PayRequest"> <value>XYZ</value> </field> From the above XML snip it, once it's sent to PHP, I would like to somehow reference the "PayRequest" field value and retrieve the value "XYZ" and assign it to a variable. I have been kicking around some code that I thought should work, but doesn't appear to. If anyone could offer any insight to get it working that would be greatly appreciated. PHP Code: Code: [Select] $prq_ret = simplexml_load_file('test.xml'); foreach($prq_ret->xpath("//field[@name='PayRequest']") as $item) { $row = simplexml_load_string($item->asXML()); //print $item->value; print $row; } Hey
Need help i am having a mental block
when I fill in the form and click send it sends the form but looks like this
Name: , Email: , Subject: , Message:the form data is <?php $name = $_POST['name']; $email = $_POST['email']; $subject = $_POST['subject']; $message = $_POST['message']; $formContent = "From: $name, \n Email: $email, \n Subject: $subject, \n Message: $message"; $recipient = "email@email.com"; $subject = "Website Request"; $mailheader = "Website \r\n"; mail($recipient, $subject, $formContent, $mailheader); echo "Your message was sent" . " - " . "<a href='index.html' style='text-decoration: none; color: #d03837;'>Return Home</a>"; ?>Anyone with any ideas |