i have a little script and i want to give possibility to the user to edit the "config.php" file , with the script in the (setting.php) page .
For Example , they are some of codes in "config.php"
$title = 'myblog';
$email = 'info#google.com';
$desc = 'Something';
i want to have a "HTML" page , to get the value of the things that i said.
(the user enter the value and the value should be used in other parts of script)
if you want something like the user can change the title, if there are more than one user means if one changes config.php the title will be changed for the rest.
so, it's better to use a databases.
if it's only one user, you can use a file to store the information in.
please, explain more what you want to do, one user or more than one ?
You should better do this:
In the settings.php file:
<form action="settings2.php" method="POST">
Title:<input type="test" name="title"><br>
Email:<input type="test" name="email"><br>
Desc:<input type="test" name="desc"><br>
<input type="submit">
And settings2.php:
<?php
$title = $_POST['title'];
$email = $_POST['email'];
$desc = $_POST['desc'];
$content= '<?php
$title = "'.$title.'";
$email = "'.$email.'";
$desc = "'.$desc.'";
?>';
$file = "config.php";
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, $content);
fclose($fh);
?>
These 2 pages will put in config.php the values submited by the form, for example:
<?php
$title = "PHP";
$email = "email#email.com";
$desc = "something";
?>
Related
The file has a 2 texboxes submit button when it is tapped the data is put into the newly created file.Each new created file
I have file j.php that creates a texfile question.$myDate.txt creates a new txt file with a unique name and $question1 represents the question and it is put in the textfile data as the first element the sign "," splits $question1 and $question2. The variable $question2 represents the answer. And is put the second element in the text file.
The second file questions.php opens instntly after the button is submitted the file is supposed the read adn split the data of the text file and read it and dispay it once the texfile is split into two elements since the text files contains two elements. The first one shows up as a question and the texbox in should be written an answer and the form would be submitted.
But the problem on file 2 it it dosen't read the textfile data. when the text box pops up.
<?php
if(isset($_POST['submit'])) {
$number =$number +1;
$myDate = round(10*microtime(TRUE));
$result ="question".$myDate.".txt";
$question1 = $_POST['name'];
$question2 = $_POST['age'];
$file = fopen( $result, "w+" ) or die( "file not open" );
$s = $question1 . "," . $question2 . "\n";
fputs($file, $s)or die("Data not written");
// Here - redirect to questions.php to display the contents.
header('Location: questions.php');
die;
}
else{
echo
'<center>
<form action = "j.php" method = "post">
<br>Question you want ask the person <input type = "text" name="name"> <br>
<br>Answer<input type = "text" name = "age"><br>
<input type = "submit" name = "submit" value = "Make Question">
</form>
</center>';}
?>
<?php
$myDate = glob("*.txt");
$myFile = "question".$myDate."txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$arr =explode(",",$theData);
$x=$arr[0];
$y = $arr[1];
$text = $_POST["name"];
$text = true;
if($_POST['submit']) {
$text = $_POST["name"];
if ( $text ==$y) {
echo "<font color = 'red'>Your answer is incorrect</font>";
} else {
echo "answer not correct";
}
}
?>
<center>
<form method = "post">
<?php echo $x ?> <input type = "text" name="name"> <br>
<input type = "submit" name = "submit" value = "Submit answer">
</form>
</center>
\\expected output the texfile being read and next to the text box the question popped upp\\
i have problems with my login page. Im still new to php and my teacher told me to create a login page using php and you read your username and password from text file.. and if the username is correct then should go to next page and if there are no exists username it will print an output says that no username.
please help..
These are my login.php
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form action="logg.php" method="POST">
<br /> Name: <input type="text" name="uname"><br /> Password: <input
type="password" name="password" size="15" maxlength="30" /> <br /> <input
type="submit" name="login" value="Post!"> <br />
</form>
</body>
</html>
And these are my login.php
<?php
if (isset($_POST['login'])){
$myFile = "accounts.txt";
$myFileLink = fopen($myFile, 'r');
$myFileContents = fread($myFileLink, filesize($myFile));
fclose($myFileLink);
echo $myFileContents;
?>
the answer only shows all the name but what my teacher asked me to do is that only read the certain names.. and i have no idea what to do.. please help
This should be like this :
I dont know what to do..
You can use regular expression to find the certain string (username = Bob) in your .txt
http://php.net/manual/en/function.preg-match.php
Or you can try to use unserialize
// set data for example
$store = array(
'username' => 'Bob',
'password' => 'YouShouldNotPass'
);
$fp = fopen('pass.txt','w');
fwrite($fp,serialize($store));
// Reading the data
$passtxt = file_get_contents('pass.txt');
$info = unserialize($passtxt);
echo $info['username'];
Or you can try json:
http://php.net/manual/en/function.json-decode.php
I hope it'll give you some intuition that how can you read specific username and match after login form submission.
$myFile = "accounts.txt";
//$string = file_get_contents($myFile); //you've to read it like this
$string = 'Hanna,2302 Lala,ha2302'; // I assume you have this data on file
$username_password = explode(' ',$string);
foreach($username_password as $u_p){
list($username,$password) = explode(',',$u_p); //map it to 2 different variable
// you can add any kind of condition to match specific user after login
echo "username = $username & password = $password <br/>";
}
Edit:
<?php
if (isset($_POST['login'])){
$myFile = "accounts.txt";
//$string = file_get_contents($myFile); //you've to read it like this
$string = 'Hanna,2302 Lala,ha2302'; // I assume you have this data on file
$username_password = explode(' ',$string);
foreach($username_password as $u_p){
list($username,$password) = explode(',',$u_p);//map it to 2 different variable
// you can add any kind of condition to match specific user after login
echo "username = $username & password = $password <br/>";
}
}
?>
Is it possible to save input data in php file without using any database?
Something like:
echo " inputted text.. ";
or
$text = "Text..";
Use fwrite, very easy :
<?php
$fp = fopen('myfile.txt', 'w');
fwrite($fp, 'this is my database without database :p ');
fclose($fp);
?>
If you want to work with a form, and extract some variables into a file you can use:
Your Form in form.html
<form action="recipient.php" method="POST">
INPUT1: <input type="text" name="text1" id="input1"><br/>
INPUT2: <input type="text" name="text2" id="input2"><br/>
FILENAME: <input type="text" name="filename" id="filename">
<button type="submit">Send now</button>
</form>
Your PHP recipient.php - without any validation checks:
<?php
$varA = "\$varA = ".$_POST['input1'].";"; // put your string into varible $varA
$varB = "\$varB = ".$_POST['input1'].";"; // put your string into varible $varA
$fileName = $_POST['filename']; // set a filename from form field
$text = $varA ." ". $varB; // add all together
$filepath = fopen('/var/www/html/'.$fileName.'.php', 'w+'); // set filepath and fopen to new PHP-file
fwrite($filepath, '<?php '. $text); // write text as PHP-file
fclose($filepath); // close file
?>
If you haven't read about them yet check HTML-Forms.
You might also want to look into arrays and serialisation.
But apart from that I highly recommend to have a look into Databases (PDO) - it safes you time and is much more secure.
So we are making in the class a sort of log. There is a input box and a button. Everytime the button is pressed, PHP will write on the text file and prints the current log. Now the text appears on the bottom, and we need to have the text appear on the top. Now how would we do that?
We tried doing this with alot of my classmates but it all resulted in weird behavours. (Like text is printed more then once, etc)
Thanks alot!
EDIT: Sorry, here is the code:
<html lang="en">
<head>
<title>php script</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form name="orderform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="text"/>
<input type="submit" value="Submit" />
<?php
//Basic variables
echo("<br/>");
$myFile = "log.txt";
$logfile = fopen($myFile,'r+');
$theData = fread($logfile,filesize($myFile));
//Cookie stuff so the username is rememberd.
$username = $_COOKIE['gebruikerscookie'];;
if(isset($_POST['username'])){
$gebruiker = $_POST['username'];
if($_COOKIE['gebruikerscookie'] == $gebruiker){
$username = $_COOKIE['gebruikerscookie'];
echo("Welcome back");
}else{
setcookie("gebruikerscookie", $gebruiker);
$username = $_COOKIE['gebruikerscookie'];
echo("Welcome dude!");
}
}
//Checks if theres something inside
if(isset($_POST['text'])){
$message = "<br/>". $username ." : " . $_POST['text'];
fwrite($logfile, $message ,strlen($message));
}
echo($theData);
?>
</form>
</body>
Check the fopen manual on modes: http://www.php.net/manual/en/function.fopen.php
Try 'r+' Open for reading and writing; place the file pointer at the beginning of the file.
Altough without any code this is hard to answer.
<?php
$contentToWrite = "Put your log content here \n";
$contentToWrite .= file_get_contents('filename.log');
file_put_contents('filename.log', $file_data);
?>
This will add the previous content of your file after your cureent content and write on your file.
Please reply if you have any doubt.
you're just missing the
fclose();
I assume, since not closing a filehandle can cause a lot of strange errors like this.
So
$myFile = "log.txt";
$logfile = fopen($myFile,'r+');
........
//Checks if theres something inside
if(isset($_POST['text'])){
$message = "<br/>". $username ." : " . $_POST['text'];
fwrite($logfile, $message ,strlen($message));
}
fclose($logfile); // close it outside the if-condition!
echo($theData);
should do the trick
$log = file('log.txt');
krsort($log);
foreach($log as $line) echo "$line<br>\n";
Hello I`m trying to echo the address where my php is so for example this is my address:
http://domain.com/index.php?route=product/product&path=20_27&product_id=41
I want a php that returns "domain.com/index.php?route=product/product&path=20_27&product_id=41"
I`ve got thus far to domain.com/index.php using :
<?php
$address = $_SERVER["HTTP_HOST"];
$address1 = $_SERVER["PHP_SELF"];
$address3 = "http://$adres$adres1";
$address4 = $adres3;
$address5 = $_SERVER["PATH_INFO"];
echo "$address4"; ?>
Can someone pls hlp ?
thx
Try this:
$url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;
That's
$address = $_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]
To find out stuff like this is general create a php file with that content:
<?php
phpinfo();
?>
Open it in your browser and scroll to the bottom.