PHP - Self-verified Page Vs. Using External Script (newbie Question)
Hi, I am very new to PHP (and web developing generally, as well). I have been passing through two different ways of verifying user input in a PHP webpage:
1- By calling a different script file, where the verification logic code is listed and then recall the referer $_SERVER['HTTP_REFERER'] page and pass the result using $_SESSION. (I understood this is basically done to avoid repeating the action in the code with every refreshment of the browser window). 2- By enclosing the verification logic code withing the same PHP page, so the page is a big mix of HTML & PHP. (I understood this is basically done, in order to keep the user input without using $_SESSION and it should save one trip of data transfer.) For me, I see both are working; still I want to learn the best coding practices. So your advice is appreciated, and please feel free to correct my, if I missed something about both methods. Similar TutorialsHi Guys Just learning php. I want to write my own simple cms to get to grips with how things work (not necessarily to use as i realise there are millions of open source cmses available that are much more secure and clever). I'm ok with capturing and storing data in a database and i know the rudmiments of php so far. What i'm not so clear about, and what's missing from all the tutorials i've read, is how you load the data stored in a database relating to a specific page. All examples i've seen simply access the database and dump all the content into a page. So lets say i stored some body text in a database table with a unique key of 4. I then click on a link somewhere in the website, lets say it's www.mysite.com/4 (lets assume i've done the clever stuff with .htaccess) What is the best way of loading that specific page content? Do you access the header url and strip out the end part? Then compare that to your database ? Or is there a better way? Sorry if this is vague i'm just after a brief explanation and i'll go do the leg work to find out how to do it Thank you! I'm very fresh to php coding, and embedding it in html, so I'm a bit lost here. I have made a script for doing backups of mysql on the QNAP box, I want to make a page to set the variables in it, so the users don't have to change directly in the script like they do now. First the page should read in the current config, and display that in the form, and then users can change the values, and press save, quite simple I'm sure for the experienced php coder :-) I'm not sure if it's a HTML or PHP question, so here goes, if it's in the wrong forum, maybe the moderator will forgive me, and move the topic to the right one. THe page I've currently created looks like this: Code: [Select] <html><body> <h1>Mysql Backup Script</h1> <?php $config=parse_ini_file("config.txt"); ?> <form action="writeconfig.php" method="post"> <p>Days to save config: <input type="text" name="configdays" value="<?php echo $config('configdays'); ?>"/> <br> <p>Name of Backup location (share): <input type="text" name="share" value="<?php echo $config('share'); ?>"/> <br> <p>MySQL Backup User: <input type="text" name="user" value=<?php echo $config('user'); ?>"/> <p>Errorlevel (0=off, 1=error, 2=error+warn, 3=error+warn+info: <select name="error"> <option>0</option> <option>1</option> <option>2</option> <option>3</option> </select> <br> <p><input type="submit" value="Save" /> </form> </body></html> The problem is that it doesn't show the variables in the value fields, but rather the php code And the writeconfig.php looks like this: Code: [Select] <?php $configdays = $_POST['configdays']; $share = $_POST['share']; $user = $_POST['user']; $error = $_POST['error']; $int_options = array("options"=>array("min_range"=>1, "max_range"=>100)); if (!filter_var($configdays, FILTER_VALIDATE_INT, $int_options)) die("Value for number of config days is incorrect allowed value is 1-100"); $fp = fopen("config.txt", "w"); fwrite($fp, "configdays=" . $configdays . "\r\n" . "share=". $share . "\r\n" . "user=". $user . "\r\n" . "error=". $error . "\r\n"); fclose($fp); echo "Config file successfully written" ?> It's all still very raw, but the write part works ok, still needs a lot of input validation of course. As you can see, I'm still in the beginning part of this, so if I'm doing something wrong, or there's another way to do it properly, let me know! My question is, when someone make same html form with action that goes on same php file on my server, so i need to know, can my site be hacked in that way ? I mean if i have form that goes on file /sources/add_user.php and when someone make same html form that goes on www.my-site.com/sources/add_user.php and in that way to add users on my baze ? in need of an external link counter script! please help cheers matt Hey, I am currently trying to get a variable created inside a require_once script to be echoed inside the main page that called the require. The script below is a basic idea of what i want to do. I just want to be able to create a basic variable none of this session stuff as its makes life harder at the moment. Thank guys, hope the snippet below gives you a better idea. Main Code: Code: [Select] <body> <?php require_once("makesVariable.php"); <div> // Variable I want to be echo "NOT WORKING" echo $var; </div> ?> </body> External PHP Code: Code: [Select] <?php //Function gets called by previous code to create the needed variable function createTheVariable(){ $var = "I am the variable to be called"; return $var; } ?> Hi, I'm REALLY new to PHP, so any help would be appreciated . I'm trying to make a script that truncates a post after a certain amount of words (Below). And I suppose the article would have to have $description="blahblahblah";. My question is; how would I go about making this script actually work? Where would I put the code and the articles? Like I said, I have less than 2 days experience with PHP, so please don't judge Code: [Select] // this signifies how to truncate function myTruncate($string, $limit, $break=".", $pad="...") { // return with no change if string is shorter than $limit if(strlen($string) <= $limit) return $string; // is $break present between $limit and the end of the string? if(false !== ($breakpoint = strpos($string, $break, $limit))) { if($breakpoint < strlen($string) - 1) { $string = substr($string, 0, $breakpoint) . $pad; } } return $string; } And the code that says when to truncate: Code: [Select] // replace 'xxx' with the number desired $shortdesc = myTruncate($description, XXX); echo "<p>$shortdesc</p>"; hi I'm a newbie in php and I came across this Code: [Select] if($_SERVER['REQUEST_METHOD'] == 'POST'){ and this Code: [Select] $_POST = $this->_params; I know this might be a stupid question but can you please tell me what they are Hello, I'm new to PHP/MySQL. Here's the situation: I have a simple little database called, "quiz." (I want to keep it simple until I figure out what I'm doing.) This database has one user, me, and two tables: Questions (QuestionID, QuestionText), and Answers (QuestionID, OptionText) QuestionID is set to autoincrement. I have one record so the biggest QuestionID is 1. Here is the php file: <?php $link = mysql_connect('PathToMySQL', 'MyUsername', 'MyPassword'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected!'; mysql_select_db(quiz); $sql = 'SELECT QuestionID, MAX(QuestionID) FROM Questions;'; // everything works great until this point // but I want the store the data into a variable so the // answer form will know what QuestionID to assign to the answers. // So I tried the following: echo $sql; // but that just echoed the query itself, not the result. // I got: Connected!SELECT "QuestionID, MAX(QuestionID) FROM Questions;" // I was expecting to see: "Connected!1" // close database mysql_close($link); ?> I need to capture the value of QuestionID to pass that to the answers. How do I do that? Thank you all for this forum, and for any answers. That PHP manual is thick and complex. I always hate being a newbie. I have been in Newbieville for several languages now; this is no different. I can say from experience that the good news is that it gets better! Richard Hollenbeck I'm just learning PHP as a first language so you'll probably see my around here quite a bit! I'm completely baffled at why this isn't working and after about 30 minutes of frustration I decided to seek some help lol. Code: [Select] require '/opt/lampp/htdocs/PHP/Chapter 3/scripts/app_config.php'; //database connection code if (mysql_connect($db_host, $username, $password) != false) { mysql_select_db("db_name"); echo "MySQL Database Selected"; } else { echo "Error connecting to database"; } app_config.php Code: [Select] <php // Database Connection Constants $db_host = ""; $username = ""; $password = ""; $db_name = "chp3"; ?> results: Notice: Undefined variable: db_host in /opt/lampp/htdocs/PHP/Chapter 3/scripts/connect.php on line 6 Notice: Undefined variable: username in /opt/lampp/htdocs/PHP/Chapter 3/scripts/connect.php on line 6 Notice: Undefined variable: password in /opt/lampp/htdocs/PHP/Chapter 3/scripts/connect.php on line 6 Any help is appreciated. After looking around the net and getting i feel close i though i would post and ask you all. I am trying to add x number of names to a database each on its own row. I found some code and have edited it and i understand most of what its doing but i cannot get it to work, it just add witch ever name was last. Here is the form code just with 3 test fields. Quote <form id="form1" name="form1" method="post" action="addplayer2.php"> <p> <input name="player[]" type="text" id="player[]" size="15" /> </p> <p> <input name="player[]" type="text" id="player[]" size="15" /> </p> <p> <input name="player[]" type="text" id="player[]" size="15" /> </p> <input type="submit" name="submit" id="submit" value="Submit" /> </form> Ok this the script i found in it unedited state, i thought i would be easy to understand if you saw it before i broke it. I tried changing the word Activity to player and the work Act to play and then changing all other referanced. Quote <?php $con = mysql_connect("localhost","Application","*******"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("CpaApp", $con); foreach($_POST['Activity'] as $row=>$Act) { $Activity=$Act; // why does this not have post? Or is it using the one above? $Position=$_POST['Position'][$row]; $StartDate=$_POST['StartDate'][$row]; $EndDate=$_POST['EndDate'][$row]; } //enter rows into database foreach($_POST['Activity'] as $row=>$Act) { $Activity=mysql_real_escape_string($Act); // Same question why no $_POST? does it use the one above? $Position=mysql_real_escape_string($_POST['Position'][$row]); $StartDate=mysql_real_escape_string($_POST['StartDate'][$row]); $EndDate=mysql_real_escape_string($_POST['EndDate'][$row]); } $involv = "INSERT INTO Involvement (Activity, Position, StartDate, EndDate) VALUES ('.$Activity.','.$Position.','.$StartDate.','.$EndDate.')"; if (!mysql_query($involv,$con)) { die('Error: ' . mysql_error()); } echo "$row record added"; mysql_close($con) ?> On filling out the fields with name lets say Paul Bill and Jack it echos that it has added x number of rows but when looking in the database it has only added the last in this case Jack. If there is anyone willing to edit the code above so it will work with my single field i would be very great full, also if someone would answer the questions i placed in the script that would be great also. Many thanks. Lister471 Hye there im branded new in php here. Im trying to develop a system based on php and mysql. I have 20 fields on my database table. Im trying to separate fields into 2 forms which is each form contain 10 fields - form_1.php(contain 10 fields) -form_2.php(contain 10 fields) form_1.php and form_2.php is connected each other which is user have to insert data in the form_1.php and then there are NEXT button that will bring user to form_2.php.In the form_2.php there will be a SUBMIT button which send data to database. Im trying to do but i cannot go through can someone show me any way to ?? Hello, I'm sorry, I know nothing about php programming but need some help - I'm sure this will be a simple question! I have created a Formidable form on Wordpress (form is named BookingTest). It has a drop down box on (called dropdown) that has a list of numbers that requires a unique value. The plugin designers have shared some code that is supposed to remove used values from the drop down box but it's not working - I don't know if I've put it in the right place or if I need to edit it or anything like that. This is there documentation: https://formidableforms.com/knowledgebase/frm_setup_new_fields_vars/?fbclid=IwAR1Zj1jWo6iqdR4Cjq8MGpTLRW01XToPR7vp_2oN4XEmQ81SLAW-sbtbYKU#kb-remove-used-options I have pasted this code in it's entirety to a Code Snippets plugin but I still get all the values appear. Do I need to amend it to refer to my form explicitly? How is this filter called? Do I need to add an action?! Really confused :( Thank you! Hi I'm trying to insert some information into my database but it tells me that the name is undefined how to I define it so that it works. Here is my code Code: [Select] <html> <body> <form action="insert.php" method="post"> name: <input type="text" name="name" /> email: <input type="text" name="email" /> password: <input type="text" name="password" /> <input type="submit" /> </form> </body> </html> <?php $link = mysql_connect('localhost','test',''); if (!$link) { die('Could not connect to MySQL: ' . mysql_error()); } mysql_close($link); $sql="INSERT INTO emails (name,email, password) VALUES ('$_POST[name]','$_POST[email]','$_POST[password]')"; mysql_close($con) ?> Hi there, I am scratching my head for ages trying to get this to work. I am adapting a script that simply outputs a floorplan image from a MySQL database. Only problem is that it outputs the <DIV> and all the title and formatting even if no image exists. I only want to output the <DIV> if an image exists. Thanks in advance for any help. Charlotte Code: [Select] <div id="property-contact"> <h2 id="h2-section" class="h2-section"><?php _e('Floor Plan',TS_DOMAIN) ?></h2> <div class="box2 clearfix"> <?php $floor_plans_image_id =get_post_meta($post->ID, "_thumbnail_id", true); $myrows = $wpdb->get_results( "SELECT guid FROM wp_posts WHERE ID=".$floor_plans_image_id ); ?> <a href="<?=$myrows[0]->guid?>" rel="prettyPhoto[gallery]"><img src="<?=$myrows[0]->guid?>" width="250px;"/></a> </div><!-- end box3 --> </div> i've tried this php script in html <head> <title>A BASIC HTML FORM</title> <? PHP $username = $_POST['username']; if ($username == "holier") { print("Welcome back, friend!!"); } else { print("You're not a memeber of this site"); } ?> </head> but nothing showing at the top of page tell me why please ? Ok sorry if this is a proper newbie question but say i have a timestamp saved in a database in a table called "news" and the field is called "date" how do i get the month and year only from example if the time stamp is 2011-03-29 13:57:05. And i want to list all news articles from the month 03 and the year 2011 how would i go about doing this? Thank you for reading I am fairly new to php coding and completely new Drupal 8 coding. I am having trouble understanding some regular notation I see in passing arguments to functions. Here is an example function mymodule_form_alter(&$form, Drupal\Core\Form\FormStateInterface $form_state, $form_id) I am not sure what "Drupal\Core\Form\FormStateInterface $form_state" means. Does is mean that $form_state will be passed in from FormStateInterface? Hi guys, im needing to grab information out of a mysql database field called "clientname" thats inside a table called "clients" how do i retrieve the clients name in html and display it? heres what i have so far, am just needing the code to retrieve... $host = localhost; $dbuser = user; $dbpass = pass; $dbname = mydb; $connection = mysql_connect($host, $dbuser, $dbpass); $db = mysql_select_db($dbname, $connection); // code to retrieve a particular clients details Cheers, I am fairly new to PHP, coming over from JAVA. In JAVA, you can select an element of an array and set it as a separate variable, such as: String name="": name = array[4]; But in PHP I am trying this: $ban = $banners[$selected_banner]; Where $selected_banner is a random number. Unfortunately, I am gettting the following error... Catchable fatal error: Object of class stdClass could not be converted to string Are arrays in PHP able to be used in such a way? What am I missing? I often see the colon ( : ) being used in syntax however, no amount of searching through my text books or checking php.net reveals to me exactly what this is used for, when it is used and the exact definition of it's value. Intuitively it seems to mean ' or ' but I can't be sure when || is the proper operator for 'or' Can anyone point me towards a url for a succinct explanation ? thanks VJ |