I have a Wordpress page where a user can book language classes. There are different booking options, so I built the page in a way that the booking form charges dinamically from URL. The DOM charges 16 different shortcodes but in front-end it's only displayed the one that user enters in the URL by setting some parameters. The only problem about this is that the page takes a lot of time to charge and display the form.
I'm trying to figure out a way to charge only one shortcode and only set dinamically the value that differs between all shortcodes. After some researching (as my PHP knowledge is limited), I tried this code, and it worked:
function php_insert() {
if( is_page( 645 ) ) {
$awQueryString = $_SERVER['QUERY_STRING'];
parse_str($awQueryString, $awQueryStringParams);
$language = $awQueryStringParams['lang'];
$pack = $awQueryStringParams['pack'];
if (empty($awQueryString)) {
echo 'The URL entered is invalid. Please refresh the page with the correct URL or contact the web admin.';
}
else if ($language == 'english') {
if ($pack == '5') {
echo do_shortcode('[ameliacatalog package=13]');
}
if ($pack == '10') {
echo do_shortcode('[ameliacatalog package=14]');
}
if ($pack == '25') {
echo do_shortcode('[ameliacatalog package=15]');
}
}
}
}
add_action('wp_enqueue_scripts', 'php_insert');
The problem now is that this page is protected by password, and this snippet charges the code at the top of the page and it doesn't wait to check that the password is correct.
I don't know if this is the best way to do this workaround. Also don't know how can I do to solve the password check and location problem.
I hope I have explained everything well.
Thanks
Related
I have five unique forms each on a page of HTML. They then go to a PHP file to send the e-mail of data. Next they go to the HTML thank you page. I am hoping to change the heading according to which form they just submitted.
For example, if they submit a review, the should read "Thank you for your review" etc.
Technically all of these are saved as php files but only the e-mail page has php items.
Like <?php echo("<p>". $mail->getMessage()."</p>"); ?>
You should redirect to another php file and pass a parameter on url. Example:
sendemail.php
<?php
/** After send the email, check what kind form is (I don't know how do you check this).
This example is just to show you: */
if ($formType == 'review') {
$type = 'review';
} else if ($formType == 'anothertype') {
$type = 'anothertype';
}
header('Location: /thankspage.php?type=' . $type);
?>
thankspage.php
<?php
$type = $_GET['type'];
if ($type == 'review') {
echo '<h1>Thanks for your review</h1>';
} else if($type == 'anothertype') {
echo '<h1>Thanks for your anothertype</h1>';
}
?>
One way put a hidden field in your forms that'll get passed with the other form data. Then put an if statement on the thank you page and echo the appropriate message.
However, that'll only work either if you change the thank you page to php or change the page that receives and processes the form data to echo the thank you message as well
So I have a form where 2 out of 6 fields are visible to the user. The user can then click a button to reveal the other fields.
Each field uses the following PHP validation (Note: the preg_match is there to make sure they have entered a space as it's a full name field):
$multipleFormErrors = array();
if (!isset($firstGuestName) || empty($firstGuestName) || !preg_match("/ /",
$firstGuestName)) {
$multipleFormErrors["firstGuestName"] = "You have not entered your full name.";
}
if (!isset($secondGuestName) || empty($secondGuestName) || !preg_match("/ /",
$secondGuestName)) {
$multipleFormErrors["secondGuestName"] = "You have not entered guest #2's full
name.";
}
if (!isset($thirdGuestName) || empty($thirdGuestName) || !preg_match("/ /",
$thirdGuestName)) {
$multipleFormErrors["thirdGuestName"] = "You have not entered guest #3's full name.";
}
And so on up until guest #6.
The results are then being echoed to the user using:
if (isset($_POST["multipleSubmit"])) {
if ($multipleFormErrors) {
echo "<div class=\"errors\">";
echo "Please fix the following errors:";
echo "<ul>";
foreach ($multipleFormErrors as $error) {
echo "<li>";
echo $error;
echo "</li>";
}
echo "</ul>";
echo "</div>";
}
}
The issue here is that all of the errors will display even if guests 3 - 6 aren't visible to the user. So If they submit the form with just the initial 2 guests filled out they will get an error because guests 3 - 6 have a value of an empty string. I think a way around this would be for PHP to detect whether the display value is set to block like you can do in JS so is this possible or do I need to do something different?
Cheers!
PHP happens on the server, Javascript happens on the client, and that's the crux of your issue. The server has no way (without a lot more coding and state tracking) to know if the client is looking at something or not.
I recommend:
Keeping your general application structure the way it is (don't do that state tracking, which would require a lot more JS/jQuery/etc)
Perhaps put your error code with the text box, so that the error only shows if the text box does
Code your system with the full realization that guests (beyond the first?) are optional, so guest checking should only occur on server side if there is a partial name (As it is, the error shows if the guest is blank, which will often happen). A blank name for guests 2-6 is probably completely legitimate.
I need some help with some logic for my buying process on my website.
We have a 4 step buying process: results, customer details, payment details, order confirmation.
The results page simply outputs prices to the screen based on some query string parameters.
I then save lots of information to PHP Sessions variables for later use.
On the 2nd stage, the customer stage, I want to output some of these session variables to the screen which for the most part works.
In my code, one of the first things I do is check the existence of one of the session variables I set on the results page, just to check we are in business and the customers quote info is saving properly.
I have set up warning emails to myself to notify me when a user lands on either the customer or payment stage of the booking process but apparently the first session variable does not exist. I then display a friendly error message asking if they have enabled cookies in their browser.
We seem to be getting a lot of these warnings emails, alarmingly high. It doesn't feel like an accurate statistic of how many customers could arrive without cookies enabled.
The email alerts me of the current URL, the ref URL if there was one, the users IP address, and an output of all Session Vars they have saved (always none of course!)
I'm just stumped what to do next - are these really users or bots hitting the results page without cookies enabled which means they'll fail the test on the next page or could it be something else?
I have session_start() on the top of each of these buying pages so it's nothing like that.
Here's my customer page:
<?php
require_once "../includes/common.php";
$quoteShared = new quoteShared();
// Check if this is a direct page hit
if (requestSession("sessionnumber") == "") {
echo $quoteShared->directHit();
die;
common.php has session_start() at the top.
function requestSession($xParam) {
$value = "";
if (isset($_SESSION[$xParam]))
{
if ($_SESSION[$xParam] != "") {
$value = $_SESSION[$xParam];
}
}
return $value;
}
You can do it in javascript also, this way :
function cookiesAreEnabled()
{
var cookieEnabled = (navigator.cookieEnabled) ? 1 : 0;
if (typeof navigator.cookieEnabled == "undefined" && cookieEnabled == 0){
document.cookie="testcookie";
cookieEnabled = (document.cookie.indexOf("testÂcookie") != -1) ? 1 : 0;
}
return cookieEnabled == 1;
}
BEST WAY PHP
<?php
session_start();
if(!isset($_GET['testing'])){
setcookie('cookietest', 'somevalue', time()+3600);
header("location: cookie.php?testing=1");
}else{
if(isset($_COOKIE['cookietest']) && $_COOKIE['cookietest'] == 'somevalue'){
echo 'cookie enabled';
}else{
echo 'cookie not enabled';
}
}
I'm having a little issue with some php logic in my main index page, that will include certain pages based on the results of some functions, mainly to do with login/logout and the first time a user logs in after registering. The php that manages the includes is below:
UPDATE: (Based on suggestions from #arjan and #bigman I've updated the code as follows. The end result is still the same).
<?php
if ($login->checkForRegisterPage()) {
include("views/pages/home.php");
// are we logged in ?
} elseif ($login->isLoggedIn()) {
// check whether account is activated
if (!$login->checkActivated()) {
include("views/pages/activate.php");
// check whether user has logged in before
} elseif ($login->checkFirstLogin()) {
include("views/pages/build_profile.php");
// check action in URL and redirect accordingly
} elseif ($checkaction->checkForBuildProfilePage()) {
include("views/pages/build_profile.php");
} elseif ($checkaction->checkForViewProfilePage()) {
include("views/pages/profile.php");
// if all else fails, load the dashboard
} else {
include("views/pages/dashboard.php");
}
} else {
// not logged in, showing the login form
include("views/pages/home.php");
}
?>
The problem is with the two functions $login->checkActivated(); and $login->checkFirstLogin(); included below:
public function checkFirstLogin() {
$checkfirstlogin = $this->db->query("SELECT first_login FROM users WHERE first_login = 'Y' AND user_name = '".$this->user_name."';");
if($checkfirstlogin->num_rows == 1) {
return true;
} else {
return false;
}
}
public function checkActivated() {
$checkactivated = $this->db->query("SELECT activated FROM users WHERE activated = 'N' AND user_name = '".$this->user_name."';");
if($checkactivated->num_rows == 1) {
return false;
} else {
return true;
}
}
When the user first logs in, these functions return the correct result and I receive the page that I want. However, after login, I can still click and travel to other links on the page e.g. checkForViewProfilePage(); looks for view=profile in the URL. The thing is in order for the logic to reach the point where it even checks for that, it would have had to get past the two functions checkActivate(); and checkFirstLogin();, which it shouldn't be able to do while those criteria are met, but it still can. I hope I'm making sense. Can anyone see an error?
Obviously my login form calls the Login class which the awkward functions are stored in, and so this would be loaded on login, but the class is included in the same way here so I'm not sure why the functions don't appear to be firing.
you can use elseif to stop the nesting from going deeper.
also, why call every function twice? Only once in the if should be enough.
I solved it. The problem was higher up in the Login class - in Login db connections are only created when a session_start() is fired, i.e. on first login. Created a new class with db connection launched with each function and everything worked as it was.
Thanks to #arjan and #bigman for the formatting tips r.e. nesting.
I'm in the middle of designing a mobile site for our main ecommerce site. Because the site is composed of inflexible legacy code I've opted to look up the users user agent string and identify them as a mobile user each page request. That way no changes to the url structure are needed. This seems to be working nicely so far.
However, I thought it may be kind of cool to use this mobile version so that users can browse our ecommerce site on facebook via iframe (the dimensions are perfect). But, unlike the mobile browsers, I am having trouble finding a persistent way to identify the user as a facebook user. I know facebook sends a $_POST variable the first time a page is viewed via iframe, and I could simply just store that in a session variable and be done with it. The issue that arises though is that what if the user visits with facebook, gets marked as a facebook user in their session, then visits our regular ecommerce site? Well, they'd still be identified as a facebook user and get served the facebook version, which is not ideal.
Maybe you can tackle the problem for another angle and test if the website is loaded from a frame or not?
This is possible with javascript:
if (top === self) {
//not a frame
} else {
//a frame
}
Not sure if it's proper etiquette to answer my own question but I found an answer which is a combo of Hassou's answer and a javascript php detection script.
The script I altered is from here:
http://snippets.bluejon.co.uk/check4-js-and-cookies/check4-js-enabled-v2-phpcode.php
Essentially the idea is to use javascript to submit a form referencing the current url, the result tells you if javascript is enabled... However, the idea can easily be altered to submit a form only if javascript returns true for being in an iframe. You can then pass in the $_POST data into the form so that the $_POST data is carried over (only needed if the $_POST data is referenced within the display layer of your application). Here's the basic idea:
<?php
/* Include head of your application goes here, this should do whatever
session handling code you have and all processing done to the $_POST variables */
// ~~~~~~Full Url Function - Works With Mod_Rewrite~~~~~~~~~ //
// important thing is function will grab all $_GET vars
function fullurlnav()
{
$fullurlsortnav = 'http';
$script_name = '';
if(isset($_SERVER['REQUEST_URI']))
{
$script_name = $_SERVER['REQUEST_URI'];
}
else
{
$script_name = $_SERVER['PHP_SELF'];
if($_SERVER['QUERY_STRING']>' ')
{
$script_name .= '?'.$_SERVER['QUERY_STRING'];
}
}
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on')
{
$fullurlsortnav .= 's';
}
$fullurlsortnav .= '://';
if($_SERVER['SERVER_PORT']!='80')
{
$fullurlsortnav .=
$_SERVER['HTTP_HOST'].':'.$_SERVER['SERVER_PORT'].$script_name;
}
else
{
$fullurlsortnav .= $_SERVER['HTTP_HOST'].$script_name;
}
return $fullurlsortnav;
}
// ~~~~~~~~~~~End Full URL Function~~~~~~~~~ //
?>
<html>
<body>
<?php
// Only run this check if user has been identified as a facebook user in their session
// or if they've been identified via the $_POST['signed_request'], which facebook sends
// upon first request via iframe.
// Doing this removes the check when it's unneeded.
if (!isset($_POST['inIframe']) && ( isset($_SESSION['inIframe']) || isset($_POST['signed_request']) ) )
{
?>
<form name="postJs" action="<?php echo fullurlnav(); ?>" method="post">
<input type="hidden" name="inIframe" value="1">
<?php
// Carry over $_POST
foreach($_POST as $key => $value)
{
echo '<input type="hidden" value="'.$value.'" name="'.$key.'" />';
}
?>
</form>
<script type="text/javascript">
<!--
// If in an iframe
if (top !== self)
{
document.postJs.submit();
}
//-->
</script>
<?php
}
elseif(isset($_POST['inIframe']) && ( isset($_SESSION['inIframe']) || isset($_POST['signed_request']) ) )
{
$_SESSION['inIframe']=1;
}
else
{
$_SESSION['inIframe']=0;
}
if ($_SESSION['inIframe']== 1){
echo 'IS in an Iframe';
}else{
echo 'IS NOT in an Iframe';
}
// echo out rest of your template code
?>
</body>
</html>
It gets a little tricky skating around your page display code output and it's workings, but that's the basic idea i have so far. Technically one could separate the form generation block from the elseif else statements below, and use those above your code before any display output, that may be easier to handle. Note that the above code is untested, just given to provide the basic idea for others with the same issue.