scootstah
Staff Alumni-
Posts
3,858 -
Joined
-
Last visited
-
Days Won
29
Everything posted by scootstah
-
Should I bother with ActiveX Object?
scootstah replied to moose-en-a-gant's topic in Javascript Help
You tell us - do you want to support IE6? -
socket_set_nonblock might be what you're looking for.
-
$i = $i++; // internally is more like $temp = $i++; // $temp = 2; $i=3 $i=$temp; // $i=2 That doesn't seem to be explaining anything. $i = 2; $p = $i++;When you say this, you are assigning $p to the value of $i before $i is incremented. So since the value of $i is 2, $p is equal to 2. After $p is assigned to the value of $i, only then does $i get incremented. On the next statement, $i is equal to 3, since it has then been incremented. Now, when you use the same variable, you are basically overriding the would-be incremented value. So if you say $i = 2; $i = $i++;you are setting $i to the value of $i again before it is incremented, which is 2. Since you're using the same variable name, you're effectively preventing the increment from happening. But if you changed it to ++$i, then you would end up with 3. $i = 2; $i = ++$i; echo $i; // $i = 3This is because, the increment is evaluated before the assignment happens. So by the time $i gets its new value, the increment has happened and the new value is 3. Hopefully that all came out in a way that makes sense. EDIT: This is kind of what is happening internally when you do $i++ vs $++i. // -- $p = $i++; $i = 2; $p = $i; // $i == 2; $p == 2; $i = $i + 1; // $i == 3; $p == 2; // -- $p = $++i; $i = 2; $i = $i + 1; // $i == 3; $p = $i; // $i == 3; $p == 3;
-
You need to add the stuff to the document before you output it. In your example, you are outputting the header and then attempting to add stuff to it afterwards....that's not going to work. This sort of thing is easily done using template engines. You could do something hacky like this: <?php function addHeader($elements) { include 'header.php'; }header.php <html> <head> <?php if (!empty($elements)) { foreach($elements as $element) { echo $element; } } ?> </head>usage addHeader(array('<title>asd</title>', '<meta itemprop="name" content="asdfasdf">'));
-
Best practice for outputting HTML from within PHP
scootstah replied to enveetee's topic in PHP Coding Help
There are some templating libraries that you could look in to. That is a surefire way to separate business and view logic. Remember to make sure that you run variables through htmlspecialchars before displaying them. -
a for loop that fetches several fields by ajax from database
scootstah replied to amirreza99's topic in PHP Coding Help
Relevant code for others: <?php if(isset($_GET['printed'])) { $printed=$_GET['printed']; $connection = mysqli_connect('localhost','root','','database') or die(mysqli_error($connection)); for ($print=$printed; $print=$printed+3; $print++) { $selectquery = "SELECT content,author FROM topics WHERE id='$printed'"; $selectresult = mysqli_query($connection,$selectquery); $selectcontent= mysqli_fetch_array($selectresult); $selectquery = "SELECT query FROM posts WHERE query='$printed'"; $selectresult = mysqli_query($connection,$selectquery ); $selectnumanswer= mysqli_num_rows($selectresult); echo $selectcontent ['content']; echo "".$selectcontent ['author']; echo "".$selectnumanswer; echo "<form method=GET action=answer-writing.php> <input type=text name=answer> <input type=hidden name=query value=$printed> <input type=submit name=answerbutton value=answerthis> </form>" ; } } else { echo '<META HTTP-EQUIV="Refresh" content="0; url=http://localhost/web-ajax.html">'; } ?> <script type="text/javascript"> var printed=1; function loadFile() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.write(xmlhttp.responseText); } } xmlhttp.open("GET","ajax-get.php?printed="+printed,true); xmlhttp.send(); } //]=]=> </script> -
So it's reading a cookie and performing an authentication check on every AJAX request? That's kind of silly, AJAX is not stateless. To answer your question, you do not need to do that on every request. Once the user is authenticated, the session should reflect that on subsequent page loads.
-
Haha, doh! Time for a coffee refill.
-
No they're not, they're in single quotes. You should get rid of the $. Like so: ... VALUES (NULL, ':firstname', ... ... $q->execute(array(':firstname' => $_POST['firstname'], ...
-
Proper code styling can go a long way to not only prevent them, but make finding errors like these much, much easier. Consider the following adjustments to your code: <?php if(isset($_POST['email'])) { function died($error) { // your error code can go here echo "We are very sorry, but there were error(s) found with the form you submitted. "; echo "These errors appear below.<br /><br />"; echo $error."<br /><br />"; echo "Please go back and fix these errors.<br /><br />"; die(); } // validation expected data exists if ( !isset($_POST['firstname']) || !isset($_POST['lastname']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['bustype']) || !isset($_POST['description']) ) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $firstname = $_POST['firstname']; // required $lastname = $_POST['lastname']; // required $email = $_POST['email']; // required $telephone = $_POST['telephone']; // not required $bustype = $_POST['bustype']; // required $description = $_POST['description']; // required $con = new PDO("mysql:host=mysql.hostinger.in;dbname=databasename",'username', 'password'); $query = "INSERT INTO `databasename` " . "(`id`, `firstname`, `lastname`, `bustype`, `email`, `telephone`, `description`, `timestamp`,) " . "VALUES " . "(NULL, '$firstname', '$lastname', '$bustype', '$email', '$telephone', '$description', CURRENT_TIMESTAMP,)" ; $q = $con->prepare($query); $q->execute(array( ':$firstname' => $_POST['firstname'], ':$lastname' => $_POST['lastname'], ':$bustype' => $_POST['bustype'], ':$email' => $_POST['email'], ':$telephone' => $_POST['telephone'], ':$description' => $_POST['description'], )); echo "<h2>Thank you for filling in our contact. We shall get back to you as soon as possible.</h2>"; $con = null; }This is much easier to read. It's a lot harder to here to miss brackets, because everything is properly indented. If you're interested, I'd recommend you check out the PSR-2 style guide.
-
week date range in calendar pagination increment
scootstah replied to hfheuhf's topic in PHP Coding Help
I believe you can achieve that if you use day 0 instead of day 1. -
So, the "v" attribute is present in both places with the team abbreviation. So you could do something like this: $teams = array( 'CAR' => 'Carolina Panthers', 'ARI' => 'Arizona Cardinals', 'ATL' => 'Atlanta Falcons', ); echo $teams[$game['v']];If $game['v'] is CAR, then you'll get Carolina Panthers.
-
Need to insert dynamic dates in url filenames
scootstah replied to eggleston's topic in PHP Coding Help
This is a good approach. It ensures that you will not have links that go nowhere if you skip a month, and prevents you from having to do file checks on every date. -
We're not here to create your projects for you. If you have specific questions, or are having trouble with something specific then we can absolutely help out with that. To create persistent authentication (being able to login on one page, and stay logged in on subsequent pages) you're going to need to know how PHP sessions work. At the beginning of all your scripts you will need to start a session with session_start(). This will allow you to pass data between pages with the $_SESSION superglobal. You will need a login script which will process a form containing a username and password. The login script will hash the password, and then do an SQL SELECT query to find rows that match that username and (hashed) password. If a row is found, then the user can be logged in. Usually you would add some sort of data to the $_SESSION to say that the user is authenticated. On subsequent pages, you can check that the data in $_SESSION shows that they are logged in. If they're not logged in, redirect back to the login page. There are hundreds of tutorials out there for PHP login systems. I recommend that you do some research and try to work this out on your own. If possible, try to find tutorials that were written within the last few years. There are still some very old and outdated ones floating around that will do you no good. After that, if you get stuck or have some more specific questions, then please feel free to come back and we will be glad to help.
-
Why do you need to run the query again if you already have the data? Also, how are you keeping a user logged in if you're not already using sessions? If you're already using sessions, then you're already reading (and maybe writing) them for every request anyway. In any case, reading/writing sessions and/or executing one MySQL query per request is absolutely not a problem.
-
There's a couple ways to solve this problem. To answer what you asked, you can convert the abbreviations into team names like this: $search = array( 'CAR', 'ARI', 'ATL', ); $replace = array( 'Carolina Panthers', 'Arizona Cardinals', 'Atlanta Falcons', ); $data = 'CAR'; $data = str_replace($search, $replace, $data); // $data = Carolina PanthersYou didn't provide a sample of what the XML looks like, so that may or may not work. If $data is just the abbreviation, like in this example, then it should work. But if it's a whole bunch of other text, then you may end up replacing parts of a word or something.
-
week date range in calendar pagination increment
scootstah replied to hfheuhf's topic in PHP Coding Help
Yes, unfortunately there is a lot of dangerous misinformation floating around out there. The problem is that users can manipulate the value of PHP_SELF, which creates an XSS vulnerability. In fact, your other variables are potentially dangerous as well (for the same reason), since you are not sanitizing them before re-displaying them in your page. -
week date range in calendar pagination increment
scootstah replied to hfheuhf's topic in PHP Coding Help
If I manually create a date with strtotime(), I believe I get what you're looking for. So, my guess is that your variables do not hold the values that you think they do. Try inspecting them before you call strtotime() from the loop. Also, please do not do this: <a href="<?php echo $_SERVER['PHP_SELF'] This is not safe! You can simply link the query string and it will be attached to the current page. <a href="?week='.($week == 52 ? 1 : 1 + $week).'&year='.($week == 52 ? 1 + $year : $year); ?>">Next Week</a> -
JS Noob: Need Help with some Bootstrap Javascript
scootstah replied to vincej's topic in Javascript Help
$('#myTab a[href="#profile"]').tab('show') // Select tab by name $('#myTab a:first').tab('show') // Select first tab $('#myTab a:last').tab('show') // Select last tab $('#myTab li:eq(2) a').tab('show') // Select third tab (0-indexed)This is just showing ways that you can select specific tabs if you wanted to. All you should need for basic tabs is this: $('#myTab a').click(function (e) { e.preventDefault() $(this).tab('show') })This will call "show" on the tab that was clicked on. -
Where are you stuck exactly? Have you written any code for this problem?
-
You're correct that if you want dynamic access control, Apache probably isn't a good fit. However, I don't really see your scenario as being a real-world problem...sounds like poor design to me. You don't have to disallow an entire directory with Apache; you can also disallow to specific files, or files in a pattern.
-
Will I have any trouble storing 200kb in memory?
scootstah replied to OM2's topic in PHP Coding Help
No, 200KB won't really cause much problems. Obviously if n * 200 (where n = number of concurrent users) is greater than the RAM you have available, you'll start having issues. -
How to load value to text field when choose value in select
scootstah replied to mrbhb's topic in PHP Coding Help
Additionally, add .change() to your selector to trigger the change event on the initial page load. $("#chedo").change(function(){ var selected = $("#chedo").val(); $("#lenh").val(selected); }).change(); -
Those cookies are Google Analytics tracking cookies; you don't need to send them in your cURL request. It looks like all you need to do is send a POST request with the following data: query=127.0.0.1&submit=QueryReplace 127.0.0.1 with the IP that you want.