I'm building a database modifying gui that executes basic queries. The user have to input the name of existing database and table name at the start. I'm using fields to take user input and then parsing them to a php script. I was using session variables to use the database name and table name in different php files. Is there a way to do this using include or include_once? or any other method that serves better than session variables in this situation? Keeping in mind that the script will be using GET to grab the input in the fields.
Yes you can. You can have your php code create a php file (with the information about the database and table) which you can then include into whichever file you wish to use those variables in. First create the form that will be used for the input, something similar to the following:
index.php:
<form method="POST" action="phpscript.php">
<input type="text" name="databaseName">
<input type="text" name="tableName">
<input type="submit" name="Submit" value="Submit">
</form>
then create the php code in the page that the form takes you to that will create the new php file:
phpscript.php:
<?php
$myFile = fopen("info.php", "w");
$myStrig = "<?php $tableName = ".$_POST['tableName']."; $databaseName = ".$_POST['databaseName']."; ?>";
fwrite($myFile, $myString);
fclose($myFile);
?>
Now whichever file that you would like to have access to variables $tableName and $databaseName would just need the following line of code:
include('info.php');
Also keep in mind that I am assuming that all the files are in the same directory. If you have them in different directories, you'll have to change the paths to those files.
Let me know if that helps you!
Related
I'm new to PHP and seem to have ran into a problem I can't seem to get around.
I have a form on a secure page that creates a PHP file to store a text value. I named this variable $text.
The Form HTML Code:
<form action="upload_title.php" method="post">
<label for="text">Title 1:</label>
<input type="text" name="text" id="text"><br>
<input type="submit" name="submit" value="Submit">
</form>
The upload_title.php then seems to store the text input as $text in filename.php:
<?php
$var_str = var_export($_POST['text'], true);
$var = "<?php\n\n\$text = $var_str;\n\n?>";
file_put_contents('filename.php', $var);
?>
This seems to be functional as the form will generate filename.php, below is an example if I typed Store into the form input and submitted on the webpage.
<?php
$text = 'Store';
?>
Now the issue I'm encountering is not being able to retrieve this stored as a attribute in separate html document, the index.html in my case.
This was my best approach to changing the title attribute of an image:
<a href="upload/1.jpg">
<img src="upload/thumb.jpg" title="<?php include 'filename.php'; echo htmlspecialchars($text); ?>" alt="" class="image0">
</a>
This does not work, but I can see my JQuery detects that this is trying to be populated but does not extract the data from filename.php on the `index.htm' page.
Thank those in advance for your advice and insight, it is sincerely appreciated.
Your issue is probably the fact that you are using an html file instead of a php file, in this case index.html.
Your server is likely not set up by default to process .html files as php so the php does not get executed.
Apart from that it is not a very good way to store your value as when the php does get executed, you introduce a security risk and you use a lot more storage than necessary. You'd better store the value in a database or text file.
Using PHP, but NOT using mysql(because I'm required not to by my mentor) I'm trying to create user login form where once submitted,
program will scan another file if information exists.
if user has no preceding info, they create one.
I'm trying to find a way to append these new user pass/username to an array, which will be located in a different file from the file containing user/pass input form.
when user goes back and fill out their newly created info and logs in,
program checks if the entered info(variables) are indeed contained inside the array.
So...I'm trying to do something like:
<?php
include "fileContainingArray.php";
print <<<here
<form method="post">
<input type="text" name="user"/>
<input type="text" name="pass"/>
<input type="submit" name="login"/>
</form>
<form method="post">
<input type="text" name="newuser"/>
<input type="text" name="newpass"/>
<input type="submit" name="createnew"/>
</form>
here;
if(isset($_POST["createnew"])){
/////append newly created login info to an array w/usernames and array w/passwords
}
if(isset($_POST["login"])){
if (in_array('username', $user) && in_array('password', $password)) {
///grants access///
}
so the question is, is it possible, and if so how can I append new variables to an existing array from a different file?
any help would be great!!thanks!!
An array from the other file should be readily accessible from the current file.
$anotherArray['index'] = $newVar;
OR
array_push($anotherArray, "index", $newVar);
However, I think you are trying to get a proper login/logout system working. In this case, you should set up a database, probably MySQL.
Variables are flushed when PHP session ends, so no permanent users can be created, and storing data in a 'text' file is very inefficient and can lead to problems with access etc.
Check http://www.php.net/manual/en/function.serialize.php. You can serialize PHP objects (e.g. a User object that you could define) into a file and then unserialize it. You could build up place to store your user data like that that is not a data base. I'm not saying that's any kind of advisable for a production environment.
I'm trying to insert an image into my database. I've read a few posts and I'm clearly doing this wrong. I know that the column datatype needs to be a blob for the image. This I have done.
My form looks like this:
<form id="Dev_test" name="Dev_test" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP']);?>" enctype="multipart/form-data">
<input type="file" name="call_file" id="call_file">
<input type="submit" name="submit" id="submit" value="SUBMIT">
</form>
This is where I've seen multiple variations of how to do this, and I even came across a post that said this might not be possible.
$query = "INSERT INTO `******`.`******` (img) values ('{$_POST['file']}')"
I know the above isn't right.
So my question(s) is/are the following, is there a reason why $_POST['file'] isn't posting, and is there a better method to insert the image into my database? As an fyi, I'm aware that inserting an image directly into the database is not the most efficient method and that there are other methods by referencing file paths.
What you want to use is php's $_FILES superglobal instead.
$content = file_get_contents($_FILES['call_file']['tmp_name']);
You may want to do virus checking on $content or the like.
I would also suggest that you store the file on the system rather than as a blob in the database and store the path to the file instead.
To store image file in a DB (which is generally a bad idea because filesystem is an db designed specially for effective storing of files) you need to read the contents of the uploaded file and then put it into the query.
In php you should use $_FILES global array to get information about uploaded files, use is_uploaded_file() to check if everything is ok with uploaded file, then you have to read the contents of the uploaded file with standard file access functions and then you MUST escape the contents of the file when inserting it to database.
I am trying to build a form in HTML that, once submitted, automatically generates a new webpage using data entered into the form.I'm usually a MATLAB and python user, so I tried them first. Matlab would parse the parse the data and save it to a new .txt file, but not a .html file. Python was much the same. After searching, I came across the suggestion to use PHP to create the new page from the form data. (Someone was using php to create user webpages with the users name, email, and a picture. I tried to adapt this to suit my needs, but it is not generating the new page as I thought it would. Instead, it just displays part of the PHP code. This is the form I made:
<form action="htmlData.php" method="post">
Product Name:
<input name="Name" size="20" type="text">
<br><br>
Project Lead Name:
<input name="PLname" size="20" type="text"> <br><br>
Team-members: <br>
<textarea name="Team_members" rows=10 cols=40> </textarea> <br><br>
Product Type: <br>
<input name="Product_Type" size="20" type="text"> <br><br>
Description: <br>
<textarea name="Description" rows=10 cols=40 type="text"> </textarea>
<br>
<br> <br>
<input value="Submit" type="submit" name="formSubmit">
<input value="Reset" type="reset">
<input value="Help" type="button" onclick="window.location.href='problems.html'">
</form>
...And this is the PHP file named htmlData.php:
ob_start();
$Name = $_POST['Name'];
$PLname= $_POST['PLname'];
$Team_members= $_POST['Team_members'];
$Product_type= $_POST['Product_type'];
$Description= $_POST['Description'];
$html = <<<HEREDOC
Product Name: $Name <br>
Project Lead: $PLname <br>
Team Members: $Team_members <br> <br>
Product Type: $Product_type <br>
Description: $Description
HEREDOC;
file_put_contents('newPage.htm', $html);
header()redirect.header('location: newPage.html')
What do I need to change so that once a user clicks submit, a new page is generated from the data and the user is then taken to the newly created page? Is this possible with what I have, or should I looking into using a different language?
It's possible with PHP and Python (perhaps MATLAB too).
Given your PHP code, there are a few small issues. First, the HEREDOC must be ended at the beginning of the line (there can't be any whitespace before the end HEREDOC).
Second, the PHP code to redirect is invalid. Try these changes:
<?php
$Name = $_POST['Name'];
$PLname= $_POST['PLname'];
$Team_members= $_POST['Team_members'];
$Product_type= $_POST['Product_type'];
$Description= $_POST['Description'];
$html = <<<HEREDOC
Product Name: $Name <br>
Project Lead: $PLname <br>
Team Members: $Team_members <br> <br>
Product Type: $Product_type <br>
Description: $Description
HEREDOC;
file_put_contents('newPage.htm', $html);
header('location: newPage.html');
exit;
The next issue you may encounter is that it cannot write to newPage.htm. You may need/want to specify a full path (e.g. /home/yoursite/public_html/files/newPage.htm).
You will need to make sure that directory is writable by the web server (as often the web server runs as a different user than your files are owned by).
Hope that helps.
If it shows a php code, that means that:
1. You dont have php installed,
2. Your http server like Apache installed.
Even if the 2 above conditions were met, the code you have is not a valid PHP code, and it would not work.
You are not yet good enough with PHP, you need to go back and learn the very basics before you try make any dynamic pages.
Instead, it just displays part of the PHP code
Seems like one of the following:
Have you installed PHP on your server?
Is the server configured to pass .php pages to be processed?
It might be worthwile to take a look here as displaying PHP code on the browser means the webserver isn't dealing with it correctly.
Once you think it's installed run phpinfo() see the docs.
You are missing an 'l' in your new file declaration:
file_put_contents('newPage.htm', $html);
Also, try physically creating the newPage.html file and place it in your project directory then see if the page is displayed or not. If this is the case I would check to see if you have PHP installed and if the server is configured to handle post. Usually it is configured to handle post or get but not usually both by default.
While not exactly solving your original problem, I hope I'm not too out of bounds with this:
Your problem is most often solved by using a database in conjunction with a language like PHP. Storing this information in a database after form submit will allow newPage.htm you have to accept GET parameters and dynamically display the corresponding data from the database. Example: newPage.html?id=245 will deliver that ID to the PHP code processing that request. PHP will use that ID to query your database for the proper information and display that to the page. This way you will not have one file for each form submission and the form data can easier to maintain. Check out something like: http://www.freewebmasterhelp.com/tutorials/phpmysql for more information.
Another aspect to keep in mind is security. Your current code puts direct user input into a file on your server. This is a huge security vulnerability. It is recommended you escape the user input.
If you not need to create new page, in start of your PHP code, remove ob_start(), and add
if(isset($_REQUEST['formSubmit'])){
....
....
}
your page will be created automaticly on submit press.
I'm newbie in PHP.I want to know that,I taking data by html form and a .php file.
like:
<form id="form1" name="form1" method="post" action="show.php">
<strong>Please Enter the Unique id</strong><br/><br/>
Unique id:
<!-- name of this text field is "tel" -->
<input name="id" type="text" id="id" />
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</html>
Then,I used show.php file to get the 'id'.like:
$id=$_POST['id'];
Is there any way to take input by php code???
Update:
In "C" we take ant input by this way
scanf("%d",a);
is there any way to do so in PHP.I think now all you may be clear what I'm trying to say??
Thanks
Yasir Adnan.
What you are you trying to get is wrong!
HTML:- It is the communicator between the user and the browser. It displays the contents according to the user input or html code.It gets data from user or from html code.
Php :- It is the communicator between server and the browser. It has the capability of collecting from some where else other than the code like mysql data base and then uses html to display the content!
Here you are asking php to do html work which is not correct!!
the html
<input name="sb_id" type="text" id="sb_id" />
php
$id=$_POST['sb_id'];
Well, you do take the input by your php code. Your variable $id took the value of $_POST['id'] which contains the input of the textfield.
After this step you can work with the variable like any other
$id = $_POST["sb_id"]; ?
Remember that $_POST["field_name"] where field_name must be match the name attribute of your <input /> tag.
the id attribute of input tag is not sent to server inside the $_POST array. It`s typically used in client-side.
You can get data in your PHP code through GET and POST parameters. Those parameters are part of the HTTP request.
The GET parameters are in the url :
http://mywebsite.com/id=3&name=test
Then you get them using:
$id = $_GET['id'];
$name = $_GET['name'];
So you can get input data through this way when people visit the URL, call it in AJAX, or call the URL in another application (like a webservice). But no matter how it's called, it's the same for you on the PHP side.
The POST parameters are in the HTTP request, you can't pass them through the URL. You can do that by using an HTML form, or by creating the HTTP request yourself. If you are using Javascript to call your PHP code (and pass data to it), you can use AJAX to do that for example. You, in your PHP code, can get the variables this way:
$id = $_POST['id'];
$name = $_POST['name'];
If you want console-style I/O, you should probably check JavaScript/AJAX. The second one will allow you to write your own wrapper that will help you to process the input by your server "on air".
The problem is, you still need to use $_POST for AJAX. And, which is more important, it's easier (and cheaper for the server) to validate and process input by JS (and to validate and process it further on the server-side after submit).
And if the question is "how can I get the variable from the needed format?", the answer is: try using regexps/parsing the string.
Oh, btw: there IS scanf() in php, and it's called 'sscanf' ('fscanf' for files).