MySQL result doesn't replace variables - php

I'm creating a language file - content modifying file - for my website so that I can change text over multiple websites in my admin panel. I have everything working except one thing. When I use a standard lang.php file with the contents like this:
$error_page_title = "$sitename | PAGE ERROR";
and use
<? echo $error_page_title ?>
I get:
example.com | PAGE ERROR to appear on my website. Perfect, exactly what I want. Reflects the $sitename in my config file and outputs the correct website name.
When I do it as a function:
function langString($lang_id) {
$lang_result = mysql_query("SELECT lang_string as phrase FROM lang WHERE lang_id='$lang_id'");
$lang_row = mysql_fetch_array($lang_result);
return $lang_row[phrase];
}
and use
<? echo langString(3); ?>
I get:
$sitename | PAGE ERROR appearing on my website. Not what I want... Arrr!
How can I get it so that my output is recognized as PHP instead of text?

echo str_replace('$sitename', $sitename, langString(3));

when using your function langString() instead of
return $lang_row[phrase];
use
return $lang_row['phrase'];
Also use <?php instead of <?, some servers cannot parse it correctly if you only use <?. probably not the problem, but never know

Related

Is it possible to output certain content on a php document and make it die only if it’s URL is present with a value of true on a MySQL database?

I am trying to setup a PHP document but I currently am looking for a way to use the die() function and display some content on every page using my global configuration file. The way I am think how it should work is that IF the requested URL (e.g. domain.com/services/disabledservice) would have /services/disabled service and a value of 1 to make the value true in a MYSQL DB.
The plan is to have the URL be collected and checked with a table than if the row has a value of 1 for status it will display a disabled page message but if it’s 0 it will load normally.
Some research I have conducted may lead be to think that using the SQL query and the if function could work for this.
The idea I have is this but it may not be correct.
<?php $pageurl = [requested URL content here]
$checkstatus = "SELECT * FROM servicestatus WHERE page =" . $pageurl . "AND status = 1";
if ($status = mysqli_query($conn, $servicestatus)) {
if (mysqli_num_rows($status) = 1) { ?> html content here
<?php }
} else { ?>
page as normal
<?php } ?>
Edit:
To explain what I am trying to do.. I am trying to fetch the URL without everything past “?” Than I am trying to use that in a DB query to check with the database if that has a value of “m” or “d” and if it has one of those values next to the URL which is being fetched it will display the appropriate error page. This is being included as part of my core configuration file which includes my “$conn” and the core values for most stuff. The problem I am facing is that when I send my URL without everything past the “?” I am not receiving my error page and everything is loading like normal.
Use any one the following php functions:
include 'path_to_the_page.php' (or) require 'path_to_the_page.php';
The difference between include and require arises when the file being included cannot be found: include will emit a warning ( E_WARNING ) and the script will continue, whereas require will emit a fatal error ( E_COMPILE_ERROR ) and halt the script.

Output Buffering: Easy way to make string out of HTML-Code

I'm currently using Chatfuel to open the index.php-file of my website which sends the user the html code into his browser. There he can register and set up his account.
An example URL might look like this:
https://my.domain.com?key_value='123456789'
Depending on if that user is a new or a existing one, I wanna present him with a different form. In order to check so, I do a simple query to the MySQL db and see if the passed on key_value is already in the db and safe true or false to a boolean. Stating the obvious: If hes not an existing user, the 'empty' form with no values should show up. If he is registered he should see the information he filled in from last time.
My idea:
At the top of my index.php I do the check whether he's an existing customer or not (Note: This is working already). Then I want to use outputbuffering to alter the html-code depending on the boolean, before it is sent to the client.
My problem:
I developed the blueprint of the website in plain html (see code below). And OB only catches it as output if its within a string. Since I use " as well as ' in the document the string gets interrupted every few lines. Is there a simple workaround to this? Because the OB function is unable to access anything within the <html>...</html> tags.
Or do i need to use redirecting after the check (in my index.php) and create a separate form + script for both edit customer data and add new customer data?
<?php
//Connection stuff
// Prepare statment: !TODO: string needs to be escaped properly first
$query_string = "SELECT * FROM tbl_customer WHERE unique_url = '$uniqueurl'";
$query_rslt = mysqli_query($conn, $query_string);
if($query_rslt == FALSE)
{
// Failure
echo "<br> Oops! Something went wrong with the querying of the db. " . $conn->connect_error;
//Handle error
}
else
{
if ($query_rslt->num_rows > 0)
{
// Set boolean
$existing_customer = TRUE;
// Create an array called row to store all tuples that match the query string
while($row = mysqli_fetch_assoc($query_rslt)) {
//...
}
}
}
// Custom post processing function
function ob_postprocess($buffer)
{
// do a fun quick change to our HTML before it is sent to the browser
$buffer = str_replace('Testing', 'Working', $buffer);
// Send $buffer to the browser
return $buffer;
}
// start output buffering at the top of our script with this simple command
// we've added "ob_postprocess" (our custom post processing function) as a parameter of ob_start
if (!ob_start('ob_postprocess'))
{
// Failure
echo "<br> Oops! Something went wrong with output buffering. Check that no HTML-Code is sent to client before calling this start function.";
// Handle error
}
else
{
// Success
// This is where the string should get accessed before sending to the client browser
echo "Testing OB.";
}
?>
<!--DOCTYPE html-->
<html lang="en">
<head>
<meta charset="utf-8">
//...
</body>
</html>
<?php
// end output buffering and send our HTML to the browser as a whole
ob_end_flush();
?>
Output: "Working OB."
EDIT: I added source code example. This code won't compile.
Since, i can't comment, so i'll put some of my question here.
I dont really get the point, but give me a try, are you mean escaping string? you can use backslashes \ to escape string.
Like this "select from ".$dbname." where id = \"".$id."\"".
You can easily using addslashes($var) before adding the variable to the sql. like this
$id = addslashes($_POST['id']);
$sql = "select form db where id = '$id'";
If you mean checking the existent of the user to select which form to show in the page, why dont you do this?
if(userCheck()) {
?>
// here write the html code if user passed
<?php
} else {
?>
// here write the html code if user not passed
<?php
}
You can put userCheck() as global function or whereever you place it, as long as you can use it when you want to check the user before showing the form.
tl;dr: The thing I was looking for was a combination of file_get_contents() and object buffering.
file_get_contents() returns a string of a plain html-file of your choice. I could post a ton of explanation here or simply link you to phppot.com. The article offers you a directly executable demo with source (Download here). In case you wanna try it with a html file of yours, simply change the file path.
So once the whole html was converted into a string, I used the postprocessing function of OB to alter the string (= basically my html) if it's an existing user that came to alter his data. Then all the html-code (in a string still at this point) is sent to the client using ob_end_flush(). I will put up the actual code asap :)

2 Include code into 1 line php sql

I have a problem with the following code. How do i put 2 included files into 1 line include?
I already tried it like the code below but nothing is shown (blank page). It should show the url.
Url: theme/default/main/index.php (it would be)
application-sql-realtime.php = (should show 'default' or anything else when user changing their themes template it connect to sql)
<?php include('./theme/<?php include_once("config/application-sql-realtime.php");?>/main/index.php');?>
Refer image for code
include_one returns boolean
Maybe can be like this:
<?php
$theme = exec("php config/application-sql-realtime.php");
include("./theme/{$theme}/main/index.php");
?>
But I think you better put it in some class/function
<?php
// inside application-sql-realtime.php you declare a function to return theme name eg: getThemeName
include("config/application-sql-realtime.php");
$theme = getThemeName();
include("./theme/{$theme}/main/index.php");
?>
Make a class with a static or non static method to retrive the value you need (e.g. 'default'). Then you can implement this value in your string to include the first file. Following an example of a static method call:
<?php
$val = EgClass::getPathPart();
include('./theme/'.$val.'/main/index.php');
?>

Include/require not working - online payment script

I have very weird problem:
I've created simple Wordpress based store. I sell e-books and send them via email. I use online payments and have plugin to work with them. It have build-in gateway in php file. I want to inject my code to this script to automatize selling. It looks like this:
<?php
//1. Get payment info from payment service via $_POST
//2. Chceck all hashes etc.
//3. Get status to variable e.g. $status
if ($status == 99)//everything is ok
{
$order_id = $_POST["no_of_transaction"];
log_to_file("Yeah i am workig. id =".$order_id."with status ".$status);
include $_SERVER["DOCUMENT_ROOT"]."/sell_stuff.php";
log_to_file("Yeah i am still workig");
}
?>
sell_stuff.php:
<?php
log_to_file("Hello i am in sending script!");
//i assume that $order_id is still visible
$order = mysql_fetch_assoc(mysql_query("SELECT * FROM order where id=".$order_id));
$mail->to = $order['email'];
$mail->attachment = "/upload/pdf/".$order['book'].'.pdf';
$mail->send();
?>
And this method not working at all :( I've tried to change include to require and use ABSPATH instead of $_SERVER["DOCUMENT_ROOT"] but it still fails. Last thing logged in log file is ""Yeah i am workig. id= xxx with status = 99".
I've created test.php file like this:
<?php
echo "Yeah i am workig";
$oder_id = 100; //my own order
include $_SERVER["DOCUMENT_ROOT"]."/sell_stuff.php";
echo "Yeah i am still workig";
?>
... and when I run this via www.mystore.com/test.php it's work perfectly. It logging to file "Hello i am in sending script!" and all other stuff.
I dont know where i make mistake :(
Try to drop "/", and use it like:
$_SERVER["DOCUMENT_ROOT"]."sell_stuff.php";
Also $_SERVER["DOCUMENT_ROOT"] is genereating path to you www root folder, like
www.serwer.com/www/
If u have your site in:
www.serwer.com/www/www2/
it will not get included. If u will use:
__DIR__."/sell_stuff.php";
it will get the path to place where your file is.
www.serwer.com/www/www2/
You can always:
var_dump($_SERVER["DOCUMENT_ROOT"]);
var_dump(__DIR__);
to see the difference and the path that you are getting.

Using WildCard in PHP if search program

this is my code that I've written so far --
<?php
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$search_link = "http://www.example.com/register_complete.php?user=";
if($actual_link == $search_link)
echo "Place your conversion pixel code here";
?>
The situation is like this - Once a user completes a registration he/she lands on urls like these -
http://www.example.com/register_complete.php?user=abcd1234
http://www.example.com/register_complete.php?user=abcd12345
http://www.example.com/register_complete.php?user=abcde123456
So what I am trying to achieve is fetch the current url using $_SERVER and then matching it with my sample url which is stored in $search_link if both these match then I wish to display a particular code otherwise the code will not be displayed anywhere else in the website.
I don't know how to use wildcard entries in PHP :(
My mind tells I should have done something like this -
http://www.example.com/register_complete.php?user=*
Can anyone over here help me regarding this please?
Why so complicated? I bet you can exclude http://www.example.com/register_complete.php, because if it's not this file, it won't load the script anyways, so focus on ?user=abc
if (isset($_GET["user"])) {
echo "Place your conversion pixel code here";
}
You can also try empty()
OR if you want to compare it to certain value:
if ($_GET["user"] == "abcd1234") {
echo "Place your conversion pixel code here";
}
There is a function which could help you with your problem and that is strpos(). Check out the following link:
http://in3.php.net/strpos

Categories