PHP writing to text file [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am working on a project for my class in college and in this project we have to write usernames and passwords to a text file. However whenever I try to run this, the information does not get written into the document. Any recommendation would be very much appreciated.
<?php
session_start();
$User = $_GET["user"];
$name = $_GET["name"];
$_SESSION["user"] = $User;
$_SESSION["name"] = $name;
$pass = rand(100000, 999999);
$file = fopen("accounts.txt", "w");
fwrite($file, $User);
fwrite($file, $pass);
fwrite($file, $name);
fclose($file);
$array = explode(" ", $name);
?>
<!DOCTYPE html>
<html>
<body>
<?php
$message = "Hello $name, you are registered. Your password is: $pass";
mail($User, "Homework Registration", $message);
print("All Done");
?>
</body>
</html>

Close the php tag after your print statement like this
print("All Done");
?>
Note :
Make sure that you have enough permissions to create and edit to the file that you do (accounts.txt)
Also turn on the error_reporting if you disabled it, so that you can debug it easily

Related

Session variables in PHP not appearing [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have read all the questions concerning this but I'm still at a loss.
Using a test script like this
// PAGE 1
<?php session_start();
echo var_dump($_SESSION) . "<br>";
$_SESSION[‘session_var’] = "stuff";
$PHPSESSID = session_id();
echo session_id() . "<br>";
?>
<html>
<head><title>Testing Sessions page 1</title></head> <body>
<p>This is a test of the sessions feature.
<form action="sessionTest2.php" method="POST">
<input type= "text" name= "form_var" value= "testing">
<input type= "submit" value= "Go to Next Page"> </form>
</body>
</html>
//PAGE 2
<?PHP session_start();
echo var_dump($_SESSION);
$session_var = $_SESSION['session_var'];
$form_var = $_POST['form_var'];
echo "session_var =" . $session_var. "</br>";
echo "form_var =" . $form_var. "<br>";
$PHPSESSID = session_id();
echo session_id();
?>
the results I get in page 2 are
array(1) { ["‘session_var’"]=> string(5) "stuff" } session_var =
form_var =testing
al89u6vu02lstp99cs4damdn04
As you can see the variable session_var can be seen in the array but is not being output to the screen where expected, and yes session_start() is at the very top of both pages.
Any ideas what may be wrong
$_SESSION[‘session_var’] = "stuff";
Is using non ascii ‘’ quote marks.
Are you using a word processor to edit your code?
Those quotes are now part of the key name, see this ["‘session_var’"].
Stick to simple ascii single and double quotes ' or "

Extra "?>" symbol in PHP file (top left corner) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have an issue with a PHP page which displays a "?>" symbol in the top left corner. It looks like this:
<?php
include_once("DBconnect.php");
$getuser = $_POST["RegUsername"];
$getpass = $_POST["Pass"];
$getrepass = $_POST["RePass"];
$getemail = $_POST["Email"];
if($getuser){
if($getpass){
if($getrepass){
if($getpass == $getrepass){
if($getemail){
$code = rand();
if(mysqli_query($link, "INSERT INTO users VALUES ('', '$getuser', '$getpass', '$getemail', '0', '$code')")){
echo "INFO1";
}
else{
echo "ERROR6";
}
}
else{
echo "ERROR5";
}
}
else{
echo "ERROR4";
}
}
else{
echo "ERROR3";
}
}
else{
echo "ERROR2";
}
}
else{
echo "ERROR1";
}
?>
And I use this jQuery function to display the PHP returned value in my HTML page:
$("#RegSubmit").click(function(){
$.post( $("#RegForm").attr("action"),
$("#RegForm :input").serializeArray(),
function(info){
$("#RegErrorLog").empty();
$("#RegErrorLog").html(info);
});
$("#RegForm").submit(function(){
return false;
});
});
I always get the "?>" in front of the PHP "ERROR" returned value.
How can I get rid of that? Or how can I return a value from the PHP file using a variable instead of echo
I guess there's a problem in your DBconnect.php file.
Apart from that... you should really think about validating values taken from Http POSTs in your PHP script, before using them in db queries.
Check if you are not printing that symbol on the included file "DBconnect.php".

PHP Function name needs to be a string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to make an incredibly basic php form that outputs some text a user puts in to a database. Looked at a couple other questions like this to make sure and from what I can tell no one else asked about this. Here's the create.php that takes input from the html.
<?php
include 'connection.php';
$firstname= $_POST('inputName');
$lastname= $_POST('inputName2');
if($_POST ['SUBMIT']) { echo "please fill out the form";
header('location: ../index.html');
} else {
mysql_query("INSERT INTO requestdata ('firstname', 'lastname')
VALUES ('$firstname', '$lastname')") or die(mysql_error());
echo "User has been added"; header ('Location: ../index.html');
}
?>
Here's the html code for the form:
<form action = "php/create.php" method = "POST">
First Name <input type ="text" name='inputName' value=""/>
Last Name <input type="text" name='inputName2' value=""/>
<br />
<input type="submit" name = "button">
</form>`
and the error I'm getting is:
"Fatal error: Function name must be a string in
mywebsite.com/php/create.php on line 8
I think your $_POST variables are wrong.
Please try $_POST['inputName'] instead of $_POST('inputName') and $_POST['inputName2'] instead of $_POST('inputName2')

Php variable not showing up? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hi I'm trying to change the picture for facebook thumbnail by having php set the image in the meta tag depending on the page. I have this code but for some reason when I go to Facebook and debug it it actually shows the variable and not what the value of the variable is. Here is my code please help thank you!
<?php
if($ifpage == 'picture.php')
{
$metaimage = '<meta property="og:image" content="http://www.quickdailylaugh.com/photos/print $picture" />';
print($metaimage);
}
?>
Your variable is inside of a string enclosed with single quotes. Single quotes will take the literal value of the string $varname and not translate the variable to it's value. You need to use double quotes. Example:
$var1 = 'test';
$foo = 'The value of var1 is: $var1'; // The value of var1 is: $var1
$bar = "The value of var1 is: $var1"; // The value of var1 is: test
To interpret the varible you must use double quotes.
<?php
if($ifpage == 'picture.php')
{
$metaimage = "<meta property=\"og:image\" content=\"http://www.quickdailylaugh.com/photos/print $picture\" />";
print($metaimage);
}
?>
I had to do another query for it to show... Thank you all for the answers here is what i used...
<?php
if($ifpage == 'picture.php')
{
$photoid = $_GET['picture'];
$sql = "SELECT * FROM photos WHERE id=$photoid";
$pic = mysql_query($sql);
while($row = mysql_fetch_array($pic))
{
$picture=$row['picture'];
}
$metaimage = "<meta property=\"og:image\" content=\"http://www.quickdailylaugh.com/photos/$picture\" />";
print($metaimage);
}else{
?>

Integrating Clean code when mixing PHP and HTML [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm finding an organisation a major problem with mixing PHP and HTML, it just looks horrible, so i'm wondering if it's a viable option to create a set of object oriented methods such as this:
class MainOO {
public $Database;
public function __construct($Server,$User, $Password, $DB){
if ($this->Database = new mysqli($Server,$User,$Password,$DB)){
return true;
}
return false;
}
public function User_Login(){
$Get_Usr_Info = $this->Database->prepare("SELECT ID, Password, Salt FROM Users WHERE Username=?");
$Get_Usr_Info->bind_param('s',$_POST['username']);
$Get_Usr_Info->execute();
$Get_Usr_Info->store_result();
$User_Number = $Get_Usr_Info->num_rows;
$Get_Usr_Info->bind_result($UserID, $Stored_Password, $Stored_Salt);
$Get_Usr_Info->fetch();
$Get_Usr_Info->close();
if ($User_Number !== 1){
$Error = "Wrong Username Specified Or Password Is Incorrect";
header ("Location: index.php?Errors=".urlencode($Error));
exit;
}
// Continue with login script
}
public function Logout(){
if (session_status() !== PHP_SESSION_DISABLED){
session_destroy();
header ("Location: LoggedOut.php");
exit;
}
}
}
Then HTML side:
<?php
include "MainOO.php";
$MainOO = new MainOO("host","user","password","database");
?>
<div class="example">
<div class="example left">
<?php
$MainOO->User_Login();
?>
</div>
</div>
It's still mixing PHP & HTML, but it's making look a hell of a lot neater than having heaps of PHP in the middle of HTML.
I'm fully aware I could migrate over to a MVC Framework (which this topic is looking like) already setup, or even use a template engine such as smarty, but I want to avoid this as much as possible.. So is this a viable option to have neater PHP code within html?
You will probably want to use an isset() in the code too
e.g
<?= (isset($variable)) ? $variable : ''; ?>
e.g if variable isset then display it otherwise display nothing

Categories