Extra "?>" symbol in PHP file (top left corner) [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 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".

Related

PHP: $_FILES returns empty [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 7 years ago.
Improve this question
Pardon if parts of this question don't make senseā€”I'm a newb to programming in general. Feel free to correct me!
Here's my problem: I'm making a PHP upload page that uses $_POST. There are two upload fields in the HTML section, both of which are optional. So here's my code for, let's say, example-upload.php:
<form action="" enctype="multipart/form-data" method="post">
<input type="file" id="upload1" name="upload1" accept=".jpg">
<input type="file" id="upload2" name="upload2" accept=".jpg">
<button type="submit" name="submit">Submit either, both, or none of these</button>
</form>
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['upload1'])) {
$filename_upload1 = $_FILES['upload1']['name'];
}
if (!empty($_POST['upload2'])) {
$filename_upload2 = $_FILES['upload2']['name'];
}
// Checks if there is no upload at all
if (!isset($filename_upload1) && !isset($filename_upload2)) {
echo 'You didn\'t upload anything and that\'s OK';
} else {
if (isset($filename_upload1)) {
move_uploaded_file($_FILES['upload1']['tmp_name'], 'path/to/file/' . $filename_upload1);
}
if (isset($filename_upload2)) {
move_uploaded_file($_FILES['upload2']['tmp_name'], 'path/to/file/' . $filename_upload2);
}
echo 'One or two files was successfully uploaded';
}
}
?>
Each time I run this, submitting either one or both files, I get the "You didn't upload anything and that's okay" message, leading me to believe that I'm doing something wrong with the $_FILES variable. The unusual thing is that I have a different form on a similar page, except with one upload field instead of two. That seems to work.
Any advice? Thank you!
There's no $_POST['upload1'] variable in your form.
Files are passed within $_FILES array.
So, in simplest case you can check $_FILES['upload1']['name']:
if (!empty($_FILES['upload1']['name'])) {
$filename_upload1 = $_FILES['upload1']['name'];
}
And the same check for upload2:
if (!empty($_FILES['upload2']['name'])) {
$filename_upload2 = $_FILES['upload2']['name'];
}
You have to check $_FILES and not $_POST
if (!empty($_FILES['upload1']))
{
$filename_upload1 = $_FILES['upload1']['name'];
}

PHP writing to text file [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 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

How to use array variables as defined variables in PHP? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I declared an array
$CLISTS=array("Add_Product"=>"products.php","Payment_Type"=>"payment.php","Shipping"=>"shipping.php");
and I defined variables
<?php
define("Add_Product",TRUE);
define("Payment_Type",FALSE);
define("Shipping",FALSE);
foreach($CLISTS as $lists=>$page)
{
if($lists==TRUE)
{
?>
<div class='alert' style="text-decoration:line-through;"><?php echo str_replace("_"," ",$lists);?></div>
<?php }
else
{
?>
<div class='alert'><?php echo str_replace("_"," ",$lists);?></div>
<?php }
}
?>
Its not working. All the div is strikes. What I did mistake?
DEFINE does not do what you think it does. Define creates a named constant.
And you cannot change your array variables with it.
Simply do:
$CLISTS['Add_Product'] = true;
$CLISTS['Payment_Type'] = false;
$CLISTS['Shipping'] = false;
To change your array variables.
You can write the logic like
foreach($CLISTS as $lists=>$page)
{
if($lists == 'Add_Product')
{
?>
Or even you can use === like
foreach($CLISTS as $lists=>$page)
{
if($lists === TRUE)
{
?>

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