-
Posts
293 -
Joined
-
Last visited
-
Days Won
5
Everything posted by jodunno
-
I wonder why you do not want to build a grid and cut a path at the same time as well as build an array to track the grid. You could easily create the grid and a grid array in one fell swoop. <?php // Grid settings $gridWidth = 5; $gridHeight = 5; $svgSize = 100; $svgColor = '#333'; $svg['center'] = '<rect width="80%" height="80%" x="10%" y="10%" rx="5%" ry="5%" />'; // Images (Icons) $iconExit = '🔒'; // Shows a lock $iconMonster = '🦖'; // Shows a dinosaur $iconStart = '🙂'; // Shows a smiley $iconTreasure = '🎁'; // Shows a gift // Calculate Monsters $monsters = ceil($gridHeight * $gridWidth * 0.15); // Calculate Treasures $treasures = floor($gridHeight * $gridWidth * 0.25); // Make iconBox and shuffleBox $iconBox = array_merge( array_fill(1, $treasures, $iconTreasure), array(1 => $iconExit), array_fill(1, $monsters, $iconMonster), array(1 => $iconStart) ); $shuffleBox = $iconBox; shuffle($shuffleBox); // Build the full grid with all information $grid = array(); for ($i = 1; $i <= $gridHeight; $i++) { for ($j = 1; $j <= $gridWidth; $j++) { $grid[$i][$j] = 'content'; $image = '<svg width="'.$svgSize.'px" height="'.$svgSize.'px" fill="'.$svgColor.'" xmlns="http://www.w3.org/2000/svg">'.PHP_EOL; $image .= ' '.$svg['center'].PHP_EOL; // Center square $image .= '</svg>'; // Close SVG echo $image; // Show image } echo '<br>'; } //echo '<br>'; print_r($grid); //echo '<br><br>'; $k = 1; foreach ($grid as $row => $association) { $l = 1; foreach ($association as $column => $data) { echo 'row ' . $k . ', column ' . $l . ': ' . $data . '<br>'; $l++; } $k++; echo '<br>'; } ?> your code seems to contradict the doors because the doors are not being used in anyway other than creating paths around a random maze. I think that this is somewhat like the old Atari game Haunted House (one of my favs.) Anyway, you could write a make path algorithm and build the doors from there, which would now only be bottom or right. I think that you are overthinking. Even better is to use mathematics to design the grid and path but it is not necessary.
-
I thought that you wanted every square to be connected, which is what i changed. I looked at your code again and i see what you are doing: the smiley is the start and the lock is the exit. The walking in a circle is doors that should be broken but all columns should not have four doors. Still, i wouldn't program it this way but it's your code. we get a better view of what is happening when we print the contents of your field array. Take a look at the array, if you have time. take a closer look at yourcheck each field = connected code. echo '<br><br>'; foreach ($field as $row => $colArr) { foreach ($colArr as $col => $door) { echo '<table cellspacing="2" cellpadding="2" border="1" width="400"><tr><td bgcolor="#f0f0f0" width="100">row</td><td bgcolor="#f0f0f0" width="100">col</td><td bgcolor="#f0f0f0" width="100">door</td><td bgcolor="#f0f0f0" width="100">content</td></tr>'; foreach ($door as $direction => $content) { //echo 'row: ' . $row . ', col: ' . $col . ', door: ' . $position . ', item: ' . $item . '<br>'; echo '<tr><td>' . $row . '</td><td>' . $col . '</td><td>' . $direction . '</td><td>' . $content . '</td></tr>'; } echo '</table>'; } } echo '<br><br>'; where is the text 'content' coming from in this array? i have yet to locate the source. meantime, row -1 is curious since that would be row 0 and there is not a row 0 (for row = 1).
-
I have tested the original script with my border control hack and the grid is complete without other changes to your script. Hopefully, this tweak solves your problems. complete script with border hack and comment around the hack.: <?php // Start Sessie //session_start(); // Map settings $mapWidth = 5; $mapHeight = 5; $squareSize = 70; $squareColor = '#333'; // Content settings $monster = '🦖'; $treasure = '🎁'; $start = '🙂'; $exit = '🔒'; // Make SVG files $fontSize = $squareSize * 2; $svg['center'] = '<rect width="80%" height="80%" x="10%" y="10%" rx="5%" ry="5%" />'; $svg['right'] = '<rect width="10%" height="20%" x="90%" y="40%" />'; $svg['bottom'] = '<rect width="20%" height="10%" x="40%" y="90%" />'; $svg['left'] = '<rect width="10%" height="20%" x="0" y="40%" />'; $svg['top'] = '<rect width="20%" height="10%" x="40%" y="0%" />'; // Function to count the values in a field function count_values($arr) { $counting = 0; foreach ($arr as $key => $val) { if (is_numeric($val)) { $counting = $counting + $val; } } return $counting; } // Calculate Monsters $monsters = ceil($mapHeight * $mapWidth * 0.16); // Calculate Chests $chests = floor($mapHeight * $mapWidth * 0.24); // Check map size if ($monsters == 0 || $chests == 0 || ($monsters + $chests + 2) > ($mapHeight * $mapWidth)) { print 'Please change your mapsize'; exit; } // Create content $squareChests = array_fill(1, $chests, 'treasure'); $squareMonsters = array_fill(1, $monsters, 'monster'); $squareStart = array(1 => 'start'); $squareEnd = array(1 => 'exit'); // Merge and mix all content $fillbox = array_merge($squareChests, $squareMonsters, $squareStart, $squareEnd); shuffle($fillbox); /******************************\ Start Grid Map \******************************/ // Build column for ($row=1; $row<=$mapHeight; $row++) { // Build row for ($col=1; $col<=$mapWidth; $col++) { $randomSquare = array(); while (count_values($randomSquare) < 1) { // Default settings $squareMaxTop = 1; $squareMaxLeft = 1; $squareMaxBottom = 1; $squareMaxRight = 1; // Top row has never a door at top if ($row == 1){ $squareMaxTop = 0; } // First column has never a door at left if ($col == 1){ $squareMaxLeft = 0; } // Last row has never a door at the bottom if ($row == $mapHeight){ $squareMaxBottom = 0; } // Last column has never a door at the right if ($col == $mapWidth){ $squareMaxRight = 0; } $randomSquare = array( 'top' => rand(0, $squareMaxTop), 'right' => rand(0, $squareMaxRight), 'bottom' => rand(0, $squareMaxBottom), 'left' => rand(0, $squareMaxLeft), ); } $field[$row][$col] = $randomSquare; } } /******************************\ Check each field = connected \******************************/ foreach ($field as $row => $colArr) { foreach ($colArr as $col => $door) { // Check top door is connected with previous top field $previousTop = $row - 1; $field[$previousTop][$col] = isset($field[$previousTop][$col]) ? $field[$previousTop][$col] : NULL; if ($field[$previousTop][$col] != NULL) { $field[$row][$col]['top'] = $field[$previousTop][$col]['bottom']; if (count_values($field[$row][$col]) < 1) { $field[$row][$col]['top'] = 1; $field[$previousTop][$col]['bottom'] = 1; } } else { unset($field[$previousTop]); } // Check right door is connected with next right field $nextRight = $col + 1; $field[$row][$nextRight] = isset($field[$row][$nextRight]) ? $field[$row][$nextRight] : NULL; if ($field[$row][$nextRight] != NULL) { $field[$row][$col]['right'] = $field[$row][$nextRight]['left']; if (count_values($field[$row][$col]) < 1) { $field[$row][$col]['right'] = 1; $field[$row][$nextRight]['left'] = 1; } } else { unset($field[$row][$nextRight]); } // Check bottom door is connected with next bottom field $nextBottom = $row + 1; $field[$nextBottom][$col] = isset($field[$nextBottom][$col]) ? $field[$nextBottom][$col] : NULL; if ($field[$nextBottom][$col] != NULL) { $field[$row][$col]['bottom'] = $field[$nextBottom][$col]['top']; if (count_values($field[$row][$col]) < 1) { $field[$row][$col]['bottom'] = 1; $field[$nextBottom][$col]['top'] = 1; } } else { unset($field[$nextBottom]); } // Check left door is connected with previous left field $previousLeft = $col - 1; $field[$row][$previousLeft] = isset($field[$row][$previousLeft]) ? $field[$row][$previousLeft] : NULL; if ($field[$row][$previousLeft] != NULL) { $field[$row][$col]['left'] = $field[$row][$previousLeft]['right']; if (count_values($field[$row][$col]) < 1) { $field[$row][$col]['left'] = 1; $field[$row][$previousLeft]['right'] = 1; } } else { unset($field[$row][$previousLeft]); } // Give dead end always a monster / treasure / Start or Exit if (count_values($field[$row][$col]) == 1 && count($fillbox) > 0) { $field[$row][$col]['content'] = $fillbox[0]; array_shift($fillbox); } else { $field[$row][$col]['content'] = ''; } } } /**************************************************\ Check broken paths If broken path, add entry to random field in broken path \**************************************************/ // How to do ????? /**************************************************\ Check walking circles If circle, remove one entry from random field in cicle path \**************************************************/ // How to do ????? // Set leftover content to random fields while (count($fillbox) > 0) { $x = rand(1, $mapHeight); $y = rand(1, $mapWidth); if ($field[$x][$y]['content'] == '') { $field[$x][$y]['content'] = $fillbox[0]; array_shift($fillbox); } } /******************************\ Show grid \******************************/ foreach ($field as $rowKey => $rowArr) { foreach ($rowArr as $colKey => $colArr) { // Create and show image SVG $image = '<svg width="'.$squareSize.'px" height="'.$squareSize.'px" fill="'.$squareColor.'" xmlns="http://www.w3.org/2000/svg">'.PHP_EOL; $image .= ' '.$svg['center'].PHP_EOL; // Center square // The doors edit: border control happens here. so essentially, the broken paths are created here with your original code. if ($rowKey == 1) { $image .= ' '.PHP_EOL; } else { $image .= ' '.$svg['top'].PHP_EOL; } if ($colKey == 1) { $image .= ' '.PHP_EOL; } else { $image .= ' '.$svg['left'].PHP_EOL; } if ($rowKey == 5) { $image .= ' '.PHP_EOL; } else { $image .= ' '.$svg['bottom'].PHP_EOL; } if ($colKey == 5) { $image .= ' '.PHP_EOL; } else { $image .= ' '.$svg['right'].PHP_EOL; } // the above code closes the doors around the grid (borders) but does not break the other doors. // The text (content) $text = ''; if ($colArr['content'] == 'monster'){ $text = $monster; } if ($colArr['content'] == 'treasure'){ $text = $treasure; } if ($colArr['content'] == 'start'){ $text = $start; } if ($colArr['content'] == 'exit'){ $text = $exit; } $image .= ' <text x="50%" y="50% "textLength="100%" font-size="'.$fontSize.'%" dominant-baseline="middle" text-anchor="middle">'.$text.'</text>'.PHP_EOL; $image .= '</svg>'; // Close SVG print $image; // Show image } print '<br>'; // Next row } ?>
-
i had some time to find the errors in your logic which control the borders, so i tweaked your code to complete the 'grid'. <?php // Start Sessie //session_start(); // Map settings $mapWidth = 5; $mapHeight = 5; $squareSize = 70; $squareColor = '#333'; // Content settings $monster = '🦖'; $treasure = '🎁'; $start = '🙂'; $exit = '🔒'; // Make SVG files $fontSize = $squareSize * 2; $svg['center'] = '<rect width="80%" height="80%" x="10%" y="10%" rx="5%" ry="5%" />'; $svg['right'] = '<rect width="10%" height="20%" x="90%" y="40%" />'; $svg['bottom'] = '<rect width="20%" height="10%" x="40%" y="90%" />'; $svg['left'] = '<rect width="10%" height="20%" x="0" y="40%" />'; $svg['top'] = '<rect width="20%" height="10%" x="40%" y="0%" />'; // Function to count the values in a field function count_values($arr) { $counting = 0; foreach ($arr as $key => $val) { if (is_numeric($val)) { $counting = $counting + $val; } } return $counting; } // Calculate Monsters $monsters = ceil($mapHeight * $mapWidth * 0.16); // Calculate Chests $chests = floor($mapHeight * $mapWidth * 0.24); // Check map size if ($monsters == 0 || $chests == 0 || ($monsters + $chests + 2) > ($mapHeight * $mapWidth)) { print 'Please change your mapsize'; exit; } // Create content $squareChests = array_fill(1, $chests, 'treasure'); $squareMonsters = array_fill(1, $monsters, 'monster'); $squareStart = array(1 => 'start'); $squareEnd = array(1 => 'exit'); // Merge and mix all content $fillbox = array_merge($squareChests, $squareMonsters, $squareStart, $squareEnd); shuffle($fillbox); /******************************\ Start Grid Map \******************************/ $squareMaxTop = 1; $squareMaxLeft = 1; $squareMaxBottom = 1; $squareMaxRight = 1; // Build column for ($row=1; $row<=$mapHeight; $row++) { // Build row for ($col=1; $col<=$mapWidth; $col++) { $randomSquare = array(); while (count_values($randomSquare) < 4) { $randomSquare = array( 'top' => rand(0, $squareMaxTop), 'right' => rand(0, $squareMaxRight), 'bottom' => rand(0, $squareMaxBottom), 'left' => rand(0, $squareMaxLeft), ); } $field[$row][$col] = $randomSquare; } } /******************************\ Show grid \******************************/ foreach ($field as $rowKey => $rowArr) { foreach ($rowArr as $colKey => $colArr) { // Create and show image SVG $image = '<svg width="'.$squareSize.'px" height="'.$squareSize.'px" fill="'.$squareColor.'" xmlns="http://www.w3.org/2000/svg">'.PHP_EOL; $image .= ' '.$svg['center'].PHP_EOL; // Center square // The doors if ($rowKey == 1) { $image .= ' '.PHP_EOL; } else { $image .= ' '.$svg['top'].PHP_EOL; } if ($colKey == 1) { $image .= ' '.PHP_EOL; } else { $image .= ' '.$svg['left'].PHP_EOL; } if ($rowKey == 5) { $image .= ' '.PHP_EOL; } else { $image .= ' '.$svg['bottom'].PHP_EOL; } if ($colKey == 5) { $image .= ' '.PHP_EOL; } else { $image .= ' '.$svg['right'].PHP_EOL; } // The text (content) $text = ''; $image .= ' <text x="50%" y="50% "textLength="100%" font-size="'.$fontSize.'%" dominant-baseline="middle" text-anchor="middle">'.$text.'</text>'.PHP_EOL; $image .= '</svg>'; // Close SVG print $image; // Show image } print '<br>'; // Next row } ?> if you run the script, you will see a complete grid with all of the fields connected without border paths. I would not code a grid like this but it is your script. I hacked the code to make it work as you suggest (unless i have misinterpreted your request.)
-
the code is doing what you have instructed it to do: create at least one door. Shouldn't you create at least 4 doors? the logic is a problem. Let's hack the script and see what happens. /******************************\ Start Grid Map \******************************/ $squareMaxTop = 1; $squareMaxLeft = 1; $squareMaxBottom = 1; $squareMaxRight = 1; // Build column for ($row=1; $row<=$mapHeight; $row++) { // Build row for ($col=1; $col<=$mapWidth; $col++) { // Default settings makes no difference here // Create random square with at least 1 door $randomSquare = array(); while (count_values($randomSquare) < 4) { $randomSquare = array( 'top' => rand(0, $squareMaxTop), 'right' => rand(0, $squareMaxRight), 'bottom' => rand(0, $squareMaxBottom), 'left' => rand(0, $squareMaxLeft), ); } $field[$row][$col] = $randomSquare; } } You need to re-examine the border control (doors at edges) and tweak the code to include 4 doors/paths.
-
how to hide data behind the = (page.php?id=xx) for the user
jodunno replied to wildware's topic in PHP Coding Help
is this your code and design or are you using someone elses code and design? valid user ids as query strings is insecure and, frankly, stupid. if you wrote the code, then i suggest a rewrite is in order. do you pass a user id query string to every page as a method of validating a logged in user? ranklist should simply use the user id in the database to query results for the valid logged-in user. you would need to rewrite your code to utilize a proper log in script and check the database each page request (shared hosting). if you have a dedicated host then use a session variable to flag a logged in user but still check the database at each page request. nota bene: if the current user is not permitted to view another user's page, then this is simply access control and you should slap yo'self for posting this question. if user_id query not equal to user_id then not permitted to view. In other words, if user_id !== user_id query McFly? Hello, McFly? try not to search the internet for solutions because you may stumble across a "pretty uri" Apache rewrite proposal, which is not pretty and it is an awful patch for even more awful design methods. Any use of Apache rewrite is alot like saying "pretend [that] you didn't see that", so silly and resource consuming. i have no idea what "ranklist" is displaying and i don't really care but it could effect resolutions. if there is a particular reason why you cannot just use a db query to control access, then you are left with patching the code. if the current user is able to view this current user id associated data, then you could use a session variable instead. However, if all of the users id associated data is accessible to a user, then this would be too many session variables used to replace ids. use a session variable with a random get_id string id as key and user_id as value check if isset session variable $_SERVER['QUERY_STRING'] (not $_GET['id']), which is now random get_id string, then display the user_id associated results. the real id is associated with the random get_id string (= key/value) you could also use a database instead of a session variable. at login, generate a random string and insert it into the user table column, named (for example) hide_user_id then, you could use that temporarily valid id instead of a session variable. query the db when loading ranklist and retrieve the correct user id to display the asociated data using this hide_user_id in the query. just be certain that the hide_user_id column has a unique constraint applied. just use your noggin and create a patch until you can rewrite the code correctly. Best Wishes, Biff. -
@Barand You, Sir, are a veteran professional programmer and it always shows in your posts. Me, on the other hand, i am more of a hacker that is slowly learning from pros. Your code is a beautiful example of a well designed form and form processing script. Bravo! Hopefully, the OP can see the beauty of your work and follow this lead. Meantime, your "Judicious application of array key names" is a perfect example of how i can use this concept in future designs. I have copied this code to a text file for future reference. Pros can easily illustrate its usefulness. Thank you for educating me on this subject . Point well noted 🙂 edit: Dear Barand, please change the following sentence "Job Done." to "Job Well Done."
-
I did not consider that someone may see my post and not understand that the code posted is just an example of how to detect empty of this array. I hope that viewers understand to escape the output after validation (code not included). I should have used the following code in my post to avoid security problems: foreach ($_POST['subquestion'] as $value) { echo htmlentities($value, ENT_QUOTES, 'UTF-8') . '<br>'; } my code sample is not a valid solution to be applied online, rather in a testing environment.
-
I have no idea why you are using inline style to hide inputs. Are you trying to implement a rogue type="hidden" format? Makes no sense, none at all. Meantime, the subquestion array in this form will never be empty because the keys will be sent in the post (with empty values). You could hack a bit to force detection but this just illustrates that your code is sloppy and unorthodox. I suggest that you read more about html forms and structures of forms. You could make a new post in the html subforum here at phpfreaks. So to show you how subquestions could be handled, i have spent a few minutes creating a script with your messy code that i had to format in addition to hacking. Here is a working attempt to detect empty or not empty and display the results. If all values are empty then the subquestion has not been populated with values, otherwise the values will be shown. For the record, i am not a fan of form input arrays. <?php declare (strict_types = 1); switch ($_SERVER['REQUEST_METHOD']) { case 'GET': ?> <form autocomplete="off" method="post"> <label for="question">Sub Question <span class="req">*</span></label> <textarea cols="46" rows="3" name="subquestion[]" placeholder="Enter Sub question here.."></textarea> <div class="divider "></div> <div class="divider"></div> <textarea class="opt_text opt_text0 " cols="46" rows="2" name="choice0[]" placeholder="Enter Choice A here.." style="display: none;"></textarea> <label for="image" class="opt_img opt_img0" style="display: none;">Add Choice A Image(if any..)</label> <input type="file" class="opt_img opt_img0" name="o-image0[]" style="display: none;"><br><br> <textarea class="opt_text opt_text0 " cols="46" rows="2" name="choice0[]" placeholder="Enter Choice B here.." style="display: none;"> </textarea> <label for="image" class="opt_img opt_img0" style="display: none;">Add Choice B Image(if any..)</label> <input type="file" class="opt_img opt_img0" name="o-image0[]" style="display: none;"><br><br> <textarea class="opt_text opt_text0 " cols="46" rows="2" name="choice0[]" placeholder="Enter Choice C here.." style="display: none;"> </textarea> <label for="image" class="opt_img opt_img0 " style="display: none;">Add Choice C Image(if any..)</label> <input type="file" class="opt_img opt_img0" name="o-image0[]" style="display: none;"> <br><br> <textarea class="opt_text opt_text0 " cols="46" rows="2" name="choice0[]" placeholder="Enter Choice D here.." style="display: none;"> </textarea> <label for="image" class="opt_img opt_img0" style="display: none;">Add Choice D Image(if any..)</label> <input type="file" class="opt_img opt_img0" name="o-image0[]" style="display: none;"> <li><label for="question">Sub Question <span class="req">*</span></label> <textarea cols="46" rows="3" name="subquestion[]" placeholder="Enter Sub question here.."> </textarea> <div class="divider "></div> <div class="divider"></div> <textarea class="opt_text opt_text1 " cols="46" rows="2" name="choice1[]" placeholder="Enter Choice A here.." style="display: none;"> </textarea> <label for="image" class="opt_img opt_img1" style="display: none;">Add Choice A Image(if any..)</label> <input type="file" class="opt_img opt_img1" name="o-image1[]" style="display: none;"><br><br> <textarea class="opt_text opt_text1 " cols="46" rows="2" name="choice1[]" placeholder="Enter Choice B here.." style="display: none;"> </textarea> <label for="image" class="opt_img opt_img1" style="display: none;">Add Choice B Image(if any..)</label> <input type="file" class="opt_img opt_img1" name="o-image1[]" style="display: none;"><br><br> <textarea class="opt_text opt_text1 " cols="46" rows="2" name="choice1[]" placeholder="Enter Choice C here.." style="display: none;"> </textarea> <label for="image" class="opt_img opt_img1 " style="display: none;">Add Choice C Image(if any..)</label> <input type="file" class="opt_img opt_img1" name="o-image1[]" style="display: none;"> <br><br> <textarea class="opt_text opt_text1 " cols="46" rows="2" name="choice1[]" placeholder="Enter Choice D here.." style="display: none;"> </textarea> <label for="image" class="opt_img opt_img1" style="display: none;">Add Choice D Image(if any..)</label> <input type="file" class="opt_img opt_img1" name="o-image1[]" style="display: none;"> <li><label for="question">Sub Question <span class="req">*</span></label> <textarea cols="46" rows="3" name="subquestion[]" placeholder="Enter Sub question here.."> </textarea> <div class="divider "></div> <div class="divider"></div> <textarea class="opt_text opt_text2 " cols="46" rows="2" name="choice2[]" placeholder="Enter Choice A here.." style="display: none;"> </textarea> <label for="image" class="opt_img opt_img2" style="display: none;">Add Choice A Image(if any..)</label> <input type="file" class="opt_img opt_img2" name="o-image2[]" style="display: none;"><br><br> <textarea class="opt_text opt_text2 " cols="46" rows="2" name="choice2[]" placeholder="Enter Choice B here.." style="display: none;"> </textarea> <label for="image" class="opt_img opt_img2" style="display: none;">Add Choice B Image(if any..)</label> <input type="file" class="opt_img opt_img2" name="o-image2[]" style="display: none;"><br><br> <textarea class="opt_text opt_text2 " cols="46" rows="2" name="choice2[]" placeholder="Enter Choice C here.." style="display: none;"> </textarea> <label for="image" class="opt_img opt_img2 " style="display: none;">Add Choice C Image(if any..)</label> <input type="file" class="opt_img opt_img2" name="o-image2[]" style="display: none;"> <br><br> <textarea class="opt_text opt_text2 " cols="46" rows="2" name="choice2[]" placeholder="Enter Choice D here.." style="display: none;"> </textarea> <label for="image" class="opt_img opt_img2" style="display: none;">Add Choice D Image(if any..)</label> <input type="file" class="opt_img opt_img2" name="o-image2[]" style="display: none;"></li> <input type="submit"> </form> <?php break; case 'POST': $countQuestions = count($_POST['subquestion']); $checkEmpty = 0; foreach ($_POST['subquestion'] as $value) { if (empty($value)) { $checkEmpty++; } } if ($checkEmpty === $countQuestions) { echo 'subquestion is empty'; break; } foreach ($_POST['subquestion'] as $value) { echo $value . '<br>'; } break; default: echo 'Request Method denied'; break; } ?> Hopefully you can create a cleaner, nicer form and process the form correctly. phpfreaks has lots of professionals that can help you when you have problems along the way. I suggest that if you continue using arrays, then atleast provide keys that can be checked for empty.
-
unset() not working within foreach loop
jodunno replied to nlomb_hydrogeo's topic in PHP Coding Help
@gizmola I am just trying to be helpful. No need to thank me, my friend. I was going to reply to this thread with similar code to your code but mac_gyver beat me to it. Best wishes. -
unset() not working within foreach loop
jodunno replied to nlomb_hydrogeo's topic in PHP Coding Help
-
Do not come here looking for handouts, then insult my friends for not providing free labor. So far as i can see, you have two answers that will solve your immediate problems. Update your code with the provided answers, then feel ashamed for your behavior. Meantime, shell games with variables serves no purpose here: $password=$_POST['password']; $dec_password=$password; focus on adding an exit after a redirect: $_SESSION['id']=$num['id']; $_SESSION['name']=$num['fname']; header("location:welcome.php"); exit; and including authorized and password columns in your query: $ret= mysqli_query($con,"SELECT id, fname, password, authorized FROM users WHERE email='$useremail'"); Yeah Glo!
-
adding to Sir Barand's statements: if you cannot figure out the JavaScript code or you're too lazy to try, then you could use CSS 3 with animations which will show and hide a span inside your div. I wonder, exactly how do you plan to fit the words "Successfully Loaded" into a 10 pixel div?
-
Urgent help needed in php image validation
jodunno replied to Dealmightyera's topic in PHP Coding Help
servers (like apache) and php have max size limits. You are trying to set a max size variable in addition to these restrictions. You will get content-length warnings regardless and POST should be empty or someone is just sending post requests to your script not using your image upload form. You are making it complicated. if (empty($_FILES['Upload']) || !empty($_FILES['Upload']['error']) && $_FILES['Upload']['error'] === 4) { //content-length warning. redirect to get } if (!is_string($_FILES['Upload']['tmp_name']) || count($_FILES) > 1) { //more than one file has been sent } if (!in_array(strtolower(pathinfo($_FILES['Upload']['name'], PATHINFO_EXTENSION)), ['jpe','jpeg','jpg','png'], true)) { //not 'jpe','jpeg','jpg','png' } -
once you follow the advice of this forums experts to solve your problem, you should definitely upgrade php and your code. I'm trying to be helpful and i hope that you take this advice, since your code has security issues involved. so working off of the same input names $_POST['uname'] and $_POST['pword'], you could easily switch to pdo: $dbHost = '127.0.0.1'; $dbName = 'iew'; $dbUser = 'root'; $dbPass = ''; $dbAttr = array( PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ); $dbConn = new PDO("mysql:host=$dbHost; dbname=$dbName; charset=utf8mb4", $dbUser, $dbPass, $dbAttr); $query = 'SELECT * FROM inno_school_staff WHERE uname = :PostUser'; $iew = $dbConn->prepare($query); $iew->execute(array(':PostUser' => $_POST['uname'])); $row = $iew->fetch(); keep in mind that you need to adjust the $dbHost, $dbName, $dbUser, and $dbPass variables to suit your working environment. now, you can load your session variables the same as before but we switched to pdo. $_SESSION['staff']['uname'] = $row['uname']; $_SESSION['staff']['fname'] = $row['fname']; $_SESSION['staff']['lname'] = $row['lname']; $_SESSION['staff']['type'] = $row['type']; $_SESSION['staff']['school'] = $row['school_name']; $_SESSION['staff']['email'] = $row['email']; $_SESSION['value_m'] = $row['value_m']; $_SESSION['value_p'] = $row['value_p']; $_SESSION['value_d'] = $row['value_d']; $_SESSION['value_b'] = $row['value_b']; however, i recommend that you hash user passwords before storing them in the database. example php script to show how this works: $formPass = "joDunn02024"; $showHash = password_hash($formPass, PASSWORD_BCRYPT); echo $showHash; Then, you can verify passwords without plaintext security issues: if (password_verify($_POST['pword'], $row['pword'])) { /* user passwords match */ } Meantime, your sessions could be intermittent if they are not properly coded. I've had this happen to me when i first started working with sessions. Even if you were to find a patch to make your site function again, it does not excuse the fact that you need to update your code and programming methods. This forum has professional programmers that can help you with any upgrade issues. I recommend that you try to update your code. Best wishes to you and i hope that you solve your problems soon.
-
admin_authenticate.php has some issues that should be noted: Regarding headers already sent, I do not see the opening html and head tags in your code, so i am assuming that you have placed them before start of session. to quote the php manual: To use cookie-based sessions, session_start() must be called before outputting anything to the browser. session_start() should be placed before any output to a browser (including html and head tags). I have no idea as to why you wouldn't have had problems before now. if ($destino == 1){print "<META HTTP-EQUIV=\"Refresh\" content=\"1;URL=admin_panels.php\">";} else {print "<META HTTP-EQUIV=\"Refresh\" content=\"1;URL=admin_panels.php\">";} the else should be different from the if branch, otherwise it doesn't make sense to me. Both uris point to admin_panels.php. Furthermore, you have a meta tag placed outside of the head element and outside of the html element. it seems as though you should be showing more code than what you have posted as well. For example, what is the php code in r_open_link.php and r_declaration.php? where is the opening html and head tags? in another php file? database gurus are going to trip over that db code laying in the middle of the floor... probably switching to pdo is a good place to begin again.
-
Urgent help needed in php image validation
jodunno replied to Dealmightyera's topic in PHP Coding Help
Post/Redirect/Get (PRG) is effective against Content-Length warnings (not visible due to the redirect) Which version of php are you using? i've tried the script on my xampp with php version 8 and only images are accepted. However, you have the acceptable image array typed twice in your script, as well as the size variable. Why even use a variable (memory consumption)? !in_array($file_extension_pp, ["png","jpg","jpeg"]) -
PHP Template Class Rendering with Arrays/Loops/PregMatch/PregReplace
jodunno replied to Solar's topic in PHP Coding Help
just to clarify: i mean that you could build an array with name and age as keys and just loop over the data. $multi2 = array(); $multi2['name'][] = $steven['name']; $multi2['name'][] = $jason['name']; $multi2['age'][] = $steven['age']; $multi2['age'][] = $jason['age']; print_r($multi2); -
PHP Template Class Rendering with Arrays/Loops/PregMatch/PregReplace
jodunno replied to Solar's topic in PHP Coding Help
why not add the arrays using the assignment operator (=)? array_push doesn't do what you want for reasons that i have forgotten. You could read the manual for a better explanation anyway. I do something similar in my nature/biology website: $multi = array(); $steven = array("name" => "Steven", "age" => 34); $jason = array("name" => "Jason", "age" => 32); $multi[] = $steven; print_r($multi); echo '<br>'; $multi[] = $jason; print_r($multi); the only problem with this method is that the multi array has its own numbered keys, thus it is now a multidimensional array. So you would need to loop over the multi array with this in mind. maybe this helps? wait for other better answers from more experienced programmers. -
common mistake, actually. Happens to everyone somewhere along the line. However, ignoring Barand's advice is probably more of a stupid mistake because it is more efficient and logical. function GetColour($Balls, $Ball) { return in_array($Ball, $Balls) ? '#0000ff': '#c53ba1'; } echo GetColour([01, 03, 27, 38, 48, 07, 12], 27) . ' = 27<br>'; echo GetColour([01, 03, 27, 38, 48, 07, 12], 73) . ' = 73'; sorry, folks: As an American born person, I prefer the stars and stripes spelling of color[-s] over the French spelling. It drives me nuts sometimes. I can excuse the British Barand but knock it off already. 🙂 LOL Honestly. Barand is a pro and that is some good pro advice for you to follow.
-
I want to add a notice for everyone using xampp on Windows in case someone is unaware of this vulnerability. one can read more details, for example, in the link below provided by Google News https://www.bleepingcomputer.com/news/security/php-fixes-critical-rce-flaw-impacting-all-versions-for-windows/ please update your versions of php and xampp (whenever a fix is provided). have a nice day, friends 🙂
-
- 1
-
"CAN'T PHP ADD 3 STUPID SIMPLE NUMBERS!???!???" ChenXiu, seriously? you have never heard of precision before? and the output of total is not "wrong, wrong, wrong", it is a product of the mathemaical process known as rounded up. PHP, as mentioned by the always helpful Requinix, is not the only programming language that is problematic with precision. The reasons lie beyond the scope of this forum but an additional tip is that even microprocessors have limitations. Maybe you have money for a supercomputer? And fractions are not 'simple' numbers by definition and they have no brains to be 'stupid' or smart for that matter. Just poking at you a bit (making fun but not being disrespectful). anyway, bcadd will help but now you may have to add additional code to determine fractions (30.50) from integers within the array, unless you intend to maintain the same format (id est: add two integers, then add a fraction to that total.) <?php $total = 0; $numbers = array( '9780393872439', '9780134130422', '30.50' ); $integers = strval($numbers[0] + $numbers[1]); $total = bcadd($integers, $numbers[2], 2); echo $total; ?> maybe you can make use of bcadd and be satisfied. But keep in mind that you will have mathematical problems with most programming languages and software restraints do not help either.
-
well, Thank you, Kicken. I have worked very hard on this project. Honestly, i learned on my own how to do this reader/scanner. I did not use anything from your code. Your code does not read addresses and jump to their offsets. I had to think about it and i came up wih a solution. I am proud of my work because i am not a programmer. I am very happy that i am reading and scanning a jpeg and accurately extracting data. I laugh because my Windows 10 does not show a date for the Blacksmith image but i have the datetime stamp from the exif data. I beat MS at this game, LOL. I am not arrogant or anything like that. I am surprised that i am able to do this. I still have to extract a few things, then i need to optimize the code etc. I started from scratch because i tried to upload an image that my Wife made using an old Motorola phone and your script almost crashed my Edge browser. I found out that the image has extra data after the EOI marker, so your code took almost 12 seconds to display the data. I decided that i need to build my own script from scratch. I needed the Motorola image to learn how the byte order works. I now process her image with no problems and even extract the exif data. the scanner is meant to be used with my upload script. I made another video to show the script in action. I tested with several code injected images and tried various file violations to see how my code handles the situation. anyway, i have learned alot by reading exif metadata turorials and thinking about how to extract this data. I finally learned how to unpack the various data types. I still need to learn how to unpack the cm resolution since it is different. I will do that eventually, my next step is getting the image dimensions, then processing the thumbnail data. Best wishes, John
-
update: I appreciate your help Kicken but maintaining two copies of the image in memory is not what i was seeking (if you strip your functions and look at the output, then you can see that the data is being copied into an array. Thus, two copies of the image are now in memory.) Also, i am not trying to build a hex viewer, so this hex dump is beyond my usage. I simply wanted to read the necessary data from the file while scanning it for code injection and weak stenography. I have spent alot of time learning how to do it myself and i am now reading the exif data and extracting the data that i would like to view. Your code lacks all of this data when parsing an App1 segment. I have uploaded a video to my youtubechannel so that you can see my new code in action. I used the Blacksmith image in this video. The video will need to be adjusted in youtube to see the results better. I have now extracted over half of the data that i was seeking. I only have to grab image dimensions, comments. I also have to calculate the dpicm when the unit is 3. I will do this eventually. In the video, you will see that i stop at the first encounter of a thumbnail in the Blacksmith photo. I still have to get the offset of the thumbnail and its length. Then i will also scan the thumbnail for code injection while reporting thumbnail count, an thumbnail image type. I am not a programmer, so this has been a long process for me. I am happy that i have written my own EXIF reader. Best wishes, John
-
or you can cut the additional three variables of a redundant second loop and get straight to the kill shot. Snipe the Snipe. <?php $ark = array( '3 cows', '7 fish', '3 cows', '1 cat' ); $received = array( '3 cows', '7 fish' ); $line = 0; foreach($ark as $expected) { echo $expected; echo !empty($received[$line]) && $received[$line] === $expected ? ' ARRIVED!' : ''; $line++; echo '<br>'; } ?> so much less code and clutter. hamster wheeling is always confusing, difficult and often redundant.