-
Posts
3,404 -
Joined
-
Last visited
-
Days Won
55
Everything posted by Ch0cu3r
-
So what are you seeing? If are you getting any error messages then post them. Have you taken any steps to debug your code?
- 10 replies
-
- php
- authentication
-
(and 2 more)
Tagged with:
-
No, not at all. The code example PravinS provided in the foreach loop has the correct logic for outputting 2 table cells per row. Although the echo after if ($i % 2 == 0) should read echo "</tr><tr>"; . You need to close the current row and then open a new row. @JakePoluis To apply PravinS solution to your loop you need to change your while loop look like this // start table and open a row echo "<table> <tr>"; $i = 0; while(...) { // output a table cell for each result echo " <td> ... output for each result ... </td>"; // close and open table row every 2 cells, NOTE replace 2 below for how many columns you want to repeat horizontally before starting a new row if (++$i % 2 == 0) { echo "\n </tr>\n <tr>"; } } // close row and table echo " </tr> </table>";
-
Need help creating a simple video file server
Ch0cu3r replied to chrisyroid's topic in PHP Coding Help
You do not need to create a new php file every folder and video file. To be able to browse the files in the directories you only need one PHP file. You start by listing the directories, as your first block of code suggests. But instead of physically linking to the directory you'd link back the php file and pass the directory as a query string parameter (eg browse.php?dir=TheSimpons) This will then list all folders listed in yoursite.com/TheSimpsons. To get the directory from the query string you'd use $_GET['dir'] $dir = isset($_GET['dir']) ? $_GET['dir'] : ''; Next we need to protect ourself from malicious users from trying to navigate our file system // protect from directory recursion $path = preg_replace('~(\.\.[\\/])+~', '', $path) . DIRECTORY_SEPARATOR; And then make sure the directory we are listing exists within the current folder (where the php script is running) if(file_exists(getcwd() . DIRECTORY_SEPARATOR . $path)) { // list folders for $path } else { echo "Folder $path does not exist"; } To list folders, add the following after // list folders for $path echo "<p>listing directories in $path"; $path = ltrim($path, '\\/') . '*'; $dirs = glob($path, GLOB_ONLYDIR); echo '<ul>'; foreach($dirs as $dir) { echo '<li><a href="?dir='.$dir.'">'.$dir.'</a></li>'; } echo '</ul>'; // list all videos Now you should be able to traverse through the directories under your video folder. To list the videos you do something similar, add the following after // list all videos $dir = rtrim($path, '\\/*'); echo "<hr /><p>listing video files in $dir"; // list all files under current directory ($_GET['dir']) $files = glob($path.'.*'); echo '<ul>'; foreach($files as $file) { $filename = basename($file); echo '<li><a href="?dir='.$dir.'&file='.$filename.'">'.$filename.'</a></li>'; } To prevent access to certain directories change if(file_exists(getcwd() . DIRECTORY_SEPARATOR . $path)) to // folders to forbid access to $forbidden_folders = array("not4u", "ignore", 'Styles'); // check that the folder is not forbidden and directory does exist if(!in_array(basename($path), $forbidden_folders) && file_exists(getcwd() . DIRECTORY_SEPARATOR . $path)) Also change echo '<li><a href="?dir='.$dir.'">'.$dir.'</a></li>'; to prevent listing forbidden folders // do not list forbidden folders if(!in_array(basename($dir), $forbidden_folders)) { echo '<li><a href="?dir='.$dir.'">'.$dir.'</a></li>'; } Similarly if you only want to list certain videos, eg mp4, wmv, mpv file etc add the following after the $forbidden_folders array // only files with these extensions will be listed $file_extensions = array('mp4', 'mpv', 'mpeg', 'wmv', 'ogv'); And then change $files = glob($path.'.*); to // find all files limited to $file_extensions $files = glob($dir.'/*.{'.implode(',', $file_extensions).'}',GLOB_BRACE); -
Because you are not getting their values from $_POST
-
No, md5 is not safe for handling passwords. If you are using PHP5.5 or newer you should be hashing your passwords using PHP's password hash library (or for older versions of PHP use ircmaxwells password_compat library). To authenticate the user you need to run a query which returns the row where the users username matches. You'd then pass the users raw password and the password hash return from the query into password_verify() to check if the user entered the correct password. Your code should look like <?php session_start(); include("mysql_connect.inc.php"); if(isset($_POST['user']) && isset($_POST['pass'])) { // use prepared statement to query the database to return the record that matches the username $stmt = $con->prepare("SELECT password FROM users WHERE username = ?"); $stmt->bind_param('s', $_POST['user']); $stmt->execute(); $stmt->bind_result($password_hash); // get the hashed password from the query result $stmt->fetch(); // verify the user entered the correct password if(password_verify($_POST['pass'], $password_hash)) { $_SESSION['username'] = $_POST['user']; echo "<p>Login success. You are logged in as: " . $_SESSION['username'] . "</p>Return to mainpage, click <a href='index.php'>here</a>!"; } else { echo "<p>Wrong username or password.</p>"; } } Ofcourse in order for this to work you need to update your existing passwords stored in your database to be hashes returned from password_hash (make sure to use the algorithm shown in example #2)
-
Boompa, CroNiX and I have explained why. it was the PLHIM.js javascript file overriding your links. That was cause of the problem!
-
Sorry what? You mean to say you want to display a default placholder image if the user has not provided one? You just need a simple if // default placeholder image $pic = 'placeholder.gif'; // if user has provided a picture, override default image if(!empty($row['ppic'])) { $pic = $row['ppic']; } echo '<a rel="facebox" href="editpic.php?id='.$row['id'].'"><img src="../'.$pic.'" width="200" height="200" style="float:left; margin-right:10px;"></a>';
-
Password Hashing - When to use password_needs_rehash
Ch0cu3r replied to thilakan's topic in PHP Coding Help
I assume you only need to call password_needs_rehash in the event PHP modifies the default hashing algorithm or you choose to use a stronger algorithm (when they become available) . For example the password hashes stored in your the database would of been generated using the old algorithm. Meaning when you go to compare the hashes they will no longer match, even though the user entered the correct password. password_needs_rehash will return true if the old hash (stored in the database) is not compatible with the new algorithm, when this happens you can safely rehash the users password to use the new algorithm and update your users password hash stored in the database so it is now compatible with the new algorithm. -
That javascript file was the cause of your issues. If we were still in the 90s then that would be of been acceptable back then. Thankfully web technologies have moved on since. You should not have any problems replicating your current menu to be HTML and CSS based. I see no reason why you'd need javascript for it. Why? PHP is a programming language - it shouldn't be used to serve only static content. If you want to use includes then use Server Side Includes. All your products information should stored in a database. You'd ideally have one file called product.php which will fetch the product details from the database and then serve that information to the user. How can we tell you what you need to do when we cant see your code.
-
Agreed. Thread locked
-
Question, are you and eyeore part of the same class? They have already posted the same question here.
-
Standard insert/update/delete code for a simple table?
Ch0cu3r replied to HenryCan's topic in PHP Coding Help
Look for tutorials on CRUD (stands for create, read, update and delete). Quick google for "PHP CRUD" found this http://www.startutorial.com/articles/view/php-crud-tutorial-part-1 That is a good starting point. If you understood the tutorial you should be able implement the comments field with ease. -
Apply anchors to your regex (^ and $). Also there is no need to also specify the characters you dont want to match. You only need to specify the characters you want to match $pattern="/^\+[\d]+$/"; $subject="+370608*05595"; // does not match, however removing the * will match $ans=preg_match($pattern, $subject, $matches); print_r($matches);
-
You need to debug your script. Have you checked to make sure the $_SESSION['SESS_FIRST_NAME']; variable contains the expected value to be used in your query? Checked that your query has not returned any errors? Have you enabled PHP error reporting?
-
You have left off the opening quote for the value attribute for the <option> tag (highlighted below) in the foreach and also closing </select> tag afterwards foreach ($k_array as $option) : print "<option value='$option->name'> {$option->name} ({$option->value}) </option>"; endforeach; print '</select>'; Also your code could be re-factored to a for loop elseif (isset($_SESSION['formation']) and isset($_SESSION['teamname'])) { print " <form method='post' action='createteam.php'>"; print "Keeper: <select name='p[]'> <option value='none'>Pick a Keeper</option>"; foreach ($k_array as $option) : print "<option value='$option->name'> {$option->name} ({$option->value}) </option>"; endforeach; print '</select>'; $possible_formations = array(442, 433, 451, 352, 343, 532); if(in_array($_SESSION['formation'], $possible_formations)) { list($defenders, $midfielders, $strikers) = str_split($_SESSION['formation']); echo '<hr>'; for($i = 0; $i < $defenders; $i++) { $x = $i + 2; print "Defender: <select name='p[]'> <option value='none'>Pick a Defender$x</option>"; foreach ($d_array as $option) : print "<option value='$option->name'> {$option->name} ({$option->value}) </option>"; endforeach; print '</select>'; } // midfielders... // strikers... print $_SESSION['formation']; } else { print "You have not picked a formation!"; } print "</br> <input type='submit' value='Submit your team'> </form>"; }
-
There should not be any reason why you would do this. POST exists for a reason. Have you google'd for "php login tutorial", that will yield tons of results. You should then be able to get idea or how to process user logins. The very basics to restricting content to authenticated users is to check the username and password against the users in your database when the login credentials are sent from your login form. When the query returns a match you set a login token in your session. For any page you want to protect you check that the login token exists in the session. If it does not exist you either redirect the user to the login page or display a warning/error message.
-
The only thing I can suggest is for you to set the extension_dir directive to the full path to your PHP extension folder, eg Also enable these directives display_errors = On display_startup_errors = On If PHP is having issues loading extensions it should pop up an error dialog when Apache is (re)started.
-
Have look at the examples shown in this thread. http://forums.phpfreaks.com/topic/11572-multi-column-results/ See if you can apply the logic demonstrated to your while loop
-
If else syntax - conditional link in form submit button
Ch0cu3r replied to johngordon's topic in PHP Coding Help
No need to state your link three times for each condition, Just define the url to be used for the link in a variable. <?php // default url $url = 'shopping_cart.php'; // override default url if cusions or cushions2 availabled if ($row['cushions'] == "-1") { $url = "accessories.php?id=" . $_REQUEST["id"]; } elseif ($row['cushions2']=="-1") { $url = "accessories2.php?id=" . $_REQUEST["id"]; } // output the link ?> <a href="<?php echo $url ?>" style="float: left; color: #251717; background-color: #DBC87B">NO THANK YOU, PROCEED WITH ORDER</a><br> -
You are getting that error because your query is most likely failing due to an error. Use mysql_error to see if your query is failing NOTE You should update code to use MySQLi or PDO. The old mysql_* functions are deprecated, which mean they are no longer supported and could soon be removed from future versions of PHP.
-
storing some values from a php-array into mysaql-db fails
Ch0cu3r replied to Maze's topic in PHP Coding Help
Sounds like your query is failing, Change line 120 to $res = $db->query($sql) or trigger_error("Query: $sql has failed! - {$db->error}", E_USER_ERROR); -
storing some values from a php-array into mysaql-db fails
Ch0cu3r replied to Maze's topic in PHP Coding Help
You have moved the closing heredoc delimiter on line 62. This is what is causing the syntax error. XML; There should not be any characters (including any whitespace characters such as spaces - except new lines) before XML; The closing delimiter must be the very first character of the next line after the heredoc statement (the xml code in your case). -
You already have the math worked out for applying the taxes (highlighted below) The number in purple is the subtotal you're applying tax to. The green number is the subtotal with 1st tax. The number in blue will be sales price (subtotal, 1st and 2nd taxes added together). So what is the problem. Are you unsure how covert it to PHP? All that is needed is for you to change the purple and green numbers to PHP variables.
-
Is that the value you get after the form has been submitted? Shouldnt you be using $_POST['enquiry'] not $enqContent when checking the value submitted from the form?
-
Unable to display output properly using php "echo" and html tags
Ch0cu3r replied to ayanda83's topic in PHP Coding Help
You are getting that result because you have an error in the HTML code being outputted. The cause of the error is leaving off the closing quote for the href attribute value, as highlighted below echo "<tr><td><a href=\"transport2.php?id=".$cntyfetchq['state']."\">".$catfetchq[1]."(<span style=\"color:red\">".$varqa."</span>)</a></td>"; NOTE: Running queries within loops is not recommended. If you have normalized your table data you should be able to get the data you require (if the data relates) from multiple tables using JOINS. Example query SELECT s.state, COUNT( b.state ) AS stateCount FROM state AS s LEFT JOIN branchaddr b ON b.state = s.state WHERE s.status =0 GROUP BY s.state ORDER BY s.state The above query fetches all states in the state table and also returns a running total of each state used in the branchaddre table. You can then process the query results using // one query used to get the data require (the state name and state count) $sqlq = " SELECT s.state, COUNT( b.state ) AS stateCount FROM states AS s LEFT JOIN branchaddr b ON b.state = s.state WHERE s.status =0 GROUP BY s.state ORDER BY s.state"; $states = mysql_query($sqlq); $i = 0; // a counter - used in the while loop // start table and open new row echo "<table border=1><tr>"; while(list($state, $stateCount) = mysql_fetch_row($states)) { // echo table cell printf('<td><a href="transport2.php?id=%s">%1$s(<span style="color:red">%s</span>)</a></td>', $state, $stateCount); // closes current row and opens a new row every 2 columns if(++$i % 2 == 0) echo '</tr><tr>'; } // close current row and table echo '</tr></table>'; This should output the states in a table like your second image