Jump to content

mac_gyver

Staff Alumni
  • Posts

    5,448
  • Joined

  • Days Won

    174

Everything posted by mac_gyver

  1. another possible reason for 'random' operation of cookies/sessions is if the hostname (with or without the www.) or path in the URL changes, depending on how you arrive at a page and then navigate around. since you are setting the cookie path parameter to a /, that leaves the hostname changing as a possible cause. you may have multiple versions of your url (with/without the www.) in links, redirects, shortcuts, bookmarks, or in your browser history, so you might be starting with one variation of the hostname and it works until you switch to a different variation of the hostname.
  2. the most common reasons for a http 500 error from a .php script is a fatal parse error or a fatal runtime error and php's error_reporting/display_errors are not turned on. just the snippet of code you posted does not produce a fatal php error, though you may have introduced such an error depending on where and how you inserted those lines into the file and something that is dependent on the result from those lines might produce a fatal runtime error if data values are missing/wrong variables... you need to set php's error_reporting to E_ALL and either set display_errors to ON or set log_errors to ON in your php.ini (for a fatal parse error you cannot set them in your code because your code never runs) to find out what if any php detected errors there are.
  3. you can turn notices off, but you shouldn't, especially when learning php as you will also miss things like typo's and using wrong variable names. you will end up wasting hours of your time trying to find simple mistakes in your code if you hide any of the types of errors. the correct coding for a variable that might not exist is to test if it isset() before using the value. using the short form of an if(){}else{} statement the code would be - $var = isset($_GET["test"]) ? $_GET["test"] : '';
  4. you would just use a foreach(){} loop. for your database query to get the calorie information, you should not run a query inside of a loop. since the foodTypes are the keys of the $_SESSION['food'] array, just get all the keys and run one query to get all the rows you need - $types = implode("','",array_keys($_SESSION['food'])); $query = "SELECT Food, Size, Calories FROM food where Food IN('$types')"; $result = mysqli_query($con,$query); $calories = array(); while($row = mysqli_fetch_assoc($result)){ $calories[$row['Food']] = $row; // store the rows using the Food as the key so that this information can be easily accessed when needed } as you loop over the $_SESSION['food'] array, the key (foodType) from this array can be used to get the corresponding calories/size information from the $calories array that was formed in the above code.
  5. by using three arrays, you are making three times too much code. do this - and all your code will be simple - if (isset($_POST['add'])){ if (empty($_POST['foodType'])){ echo 'You need to select some food!'; } else { if(!isset($_SESSION['food'][$_POST['foodType']])){ $_SESSION['food'][$_POST['foodType']] = array('Quantity' => $_POST['fooDquantity'], 'Unit' => $_POST['quantityValue']); } } }
  6. because you are using a class method, that also contains quotes as part of its syntax, inside of a php string, you need to enclose it in { } so that php can properly parse all of it - {$date->format('Y-m-d')} if you had php's error_reporting/display_errors turned on you would be getting a notice message at the use of $date->format('Y-m-d') because php thinks it is a reference to a property, not a reference to a method because php needs to be told that the ('Y-m-d') is part of the variable instead of being part of the string it is in. you should also form sql query statements in php variables so that you can echo them out to see exactly what they are for debugging purposes.
  7. the $_FILES array is empty when you try to upload a file that is larger than the post_max_size setting. $_FILES['channel_pic'] won't be set and all your code is bypassed. you need to test for this condition first. next, even if you upload a file smaller than the post_max_size setting, the upload can fail with an error. you must test if $_FILES['channel_pic']['error'] is set and equal to (an == comparison) a zero to insure that the upload actually worked. you can also test if $_FILES['channel_pic']['error'] is exactly equal to (an === comparison) a zero to combine both tests into one.
  8. php is a server-side language. all the php code in a file runs on the server when the page gets requested. only the resulting output (html, javascript, media) is send to the browser. javascript is a client-side language. all the javascript code runs in the browser after it has been received by the browser. to get a value from javascript (in the browser) to php (on the server), you must send that value as part of a http request from the browser to the server. you can either send it as $_POST or $_GET data and if you don't want the page to refresh, you would use AjAX to send the http request.
  9. do you have php's error_reporting set to E_ALL and display_errors set to ON so that things like header errors on the setcookie() statement would be reported and displayed? you may be outputting something on the page(s) that is preventing the setcookie() statement from working, depending on how you are reaching the code. you should log the token values with a time stamp so that you can determine which ones are the correct ones/what order they are generated in.
  10. the portion of the query you posted only calculates the distance. how are you using that value in the query to determine which rows are returned?
  11. you do realize that if you don't understand the basics of what you are trying to do or the direction given in the replies that you should probably just hire someone to code this for you? the 'jargon' used in any activity isn't to exclude people, it's to shorten the amount of time it takes to communicate an idea, so that someone doesn't have to write a book each time to cover the basics, which is why there are books, tutorials, and classes to learn the basics from. initializing a (session) variable to a specific state if it doesn't exist - session_start(); // initialize to an empty array if it doesn't exist if(!isset($_SESSION['used'])){ $_SESSION['used'] = array(); } since you need to reference the SoalID at the time you echo the random question, you either need to explode the data string and get the first value or you can use the SoalID as the index for your $arr array when you retrieve the data from the query. assuming you can get the id value for the question you are echoing, storing it to the $_SESSION['used'] array - $_SESSION['used'][] = $SoalID;
  12. here's some coding hints for doing this using php code - 1) use fgetcsv() for reading/parsing each line from the csv file. 2) since you need a count of each street address, you would need to read through all the data once, storing the street addresses into an array, then use array_count_values() to get the count of each street address. 3) read through the data a second time to process it. for each street address, the array obtained in the above step tells you how many times that address exists in the data. you can take that count and the status value to produce the logic needed to ignore/remove any rows.
  13. the undefined index error is because you need to -
  14. try it with the <table></table> tags inside the <form></form> tags.
  15. since you have provided the bigger picture of how you are using the information (outputting one question at a time), the method to prevent duplicates must be different. you must 'remember' which questions have been output and remove them from the 'pool' of questions. which is what i suggested in post #3 in this thread. i would store the SoalID id values in an array in a session variable, then don't select those ids in the query. if $_SESSION['used'] is an array of the id's of the questions that have been output (initialize to an empty array if it doesn't exist), adding the following to the query would only return questions that have not been used - $ids = implode(',',$_SESSION['used']); $not_in = empty($ids) ? '' : " AND SoalID NOT IN($ids)"; $sqlText="SELECT * FROM soal WHERE Difficulty='$levelStr' AND Topic='$topic' $not_in"; the previous code i posted would also need to simply output one (the first) element from the shuffled array (i.e. no foreach() loop.)
  16. if you check your result, it is only using the leading field in the TIME value (the hours.) you need to actually use a function that can perform math on a time value, generally by converting to a single base unit (seconds), performing the math, then converting back to a TIME value.
  17. you need to troubleshoot and find out what exactly is in $_COOKIE["viewed_articles"]. it's either empty or it's escaped, due to what moylin posted above, and it produced a notice error when you tried to unseralize it.
  18. i'm pretty sure that since the recaptcha signup process makes it clear you are doing something specific to a domain, that the OP would have mentioned if he had switched where he was trying to run his code. the most likely cause is still - have you validated your form page at - validator.w3.org ? the recaptcha is sensitive to having valid <table> tags within your form (not around the form.)
  19. does the 'view source' of your form page in your browser have the recaptcha html markup in it?
  20. it's likely that your form is invalid html and isn't submitting the the expected data. what does the following show (added inside your form processing logic) - echo '<pre>',print_r($_POST,true),'</pre>';
  21. when a method='get' form is submitted, only the form fields are append to the url. you would need to pass any existing get parameters as hidden form fields.
  22. there's nothing in the posted method that would prevent handling the ids. in fact, all the code is simpler by using the id as the array index.
  23. lol, jazzman, you might need to test your vision too - tiles: 1.a thin slab or bent piece of baked clay, sometimes painted or glazed, used for various purposes, as to form one of the units of a roof covering, floor, or revetment. 2.any of various similar slabs or pieces, as of linoleum, stone, rubber, or metal.
  24. you need to generate unique one-time tokens that are output in forms/links that are 'consumed' when they are used. by only producing a token at the appropriate point (a token produced at the point of starting an attack on a specific recourse, can only be used to carry out it's corresponding action on that resource) and by marking the token as being used once it has been submitted, you insure that someone at least visited the correct starting point to carry out an action and that they can only perform that action once per visit to that starting point. by keeping a recent history of the tokens, the action/resource the are for, and a timestamp of when they were created and used, you can create logic to prevent some of the automated flooding.
  25. one possible way of doing this - <?php session_start(); if(!isset($_SESSION["cart"])){ // create cart if it doesn't exist $_SESSION["cart"] = array(); } $add2cart = isset($_GET["add2cart"]) ? (int)trim($_GET["add2cart"]) : 0; // condition input if($add2cart > 0){ // a valid value $query = "select COUNT(*) from products where id=$add2cart"; // run the query using your favorite database library functions, check for errors, and retrieve the COUNT value if($count==0){ echo 'the product is not in our database'; // it is usually cleaner/clearer to handle the error condition up front in the logic } else { if(!isset($_SESSION["cart"][$add2cart])){ // check if id is not already in cart $_SESSION["cart"][$add2cart] = 1; // create item in cart } else { $_SESSION["cart"][$add2cart]++; // increment item in cart } } } // display raw cart data for debugging purposes echo '<pre>',print_r($_SESSION['cart'],true),'</pre>';
×
×
  • 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.