Keep get parameter in all url - url

I'm using prestashop and i want to run a/b test. It means you test 2 different templates of your website in the same time as the traffic is redirected 50% to templateA and 50% to templateB.
I have to theme in my folder :
-ThemeA
-ThemeB
To run the test i need to access themeB via a different url form themeA.
So in the folder "/config" i modified the file setting.inc.php like this :
if(isset($_GET['ab']))
{
define('_THEME_NAME_', 'themeB');
}
else{
define('_THEME_NAME_', 'themeA');
}
Now i can reach themeb by typing : www.myshop.com/?ab
My problem is, whenever i click a link, the ?ab parameter is lost and it goes back to template A.
I found someone using another cms that resolved to problem using this code :
`$this->redirect(array('name')); `
But i don't know how to adapt it to prestashop and where should i put it.
Any help would be very appreciated.
Thanks

One quick workaround might be using session, if session is started ([http://sg3.php.net/manual/en/function.session-start.php][1]) before your "theme check".
// Page => watermelons.php
<?php
session_start();
if(isset($_GET['ab']) || isset($_SESSION['ab'])) {
$_SESSION['ab'] = 'themeB';
define('_THEME_NAME_', 'themeB');
} else{
define('_THEME_NAME_', 'themeA');
}
echo _THEME_NAME_;
?>
<br />go to coconuts
// Page => coconuts.php
<?php
session_start();
if(isset($_GET['ab']) || isset($_SESSION['ab'])) {
$_SESSION['ab'] = 'themeB';
define('_THEME_NAME_', 'themeB');
} else{
define('_THEME_NAME_', 'themeA');
}
echo _THEME_NAME_;
?>
<br />go to watermelons

Related

How to form an URL in LWC Community Page that can carry User Info?

I am forming a web app in lightning Community Experience Builder using LWC, which already has an URL that carries the domain of my org.
Now I want to handover the URL to users along with an Id appended to its end, such that when the user visits the site I can retrieve the Id from URL using JS.
example,
the original URL: cs-123.aig.lightning.force.com/form
User lands with: cs-123.aig.lightning.force.com/form?userId=123
I must be able to retrieve the userId when the component loads using renderedCallBack or connectedCallBack.
Thanks in advance.
Note:Lightning Navigation Service offered by LWC,doesnt work outside Salesforce,LEX.
Plain JavaScript URLSearchParams should be enough. I have something similar on my project and works ok in community, with LWC embedded on the page.
connectedCallback() {
// Try to read the preselected id from URL
if (window.location.href) {
try {
let url = new URL(window.location.href);
let id = url.searchParams.get('id');
if (id) {
this.userId = id;
}
} catch (e) {
if (console) {
console.log(JSON.stringify(e));
}
}
}
}

Can I populate GET requests safely?

I am adding random get requests to URLs.
foo.com/?boofoo=foo
Can I do this safely? Or is is possible I might break a site by doing this?
You should be fine if you handle the input properly.
So for example with PHP:
<?php
if($_GET['booofooo'] == "value1") {
// do something
} elseif($_GET['booofooo'] == "value2") {
// do something
} elseif($_GET['booofooo'] == "value3") {
// do something
} else {
// error
}
?>
Also, make sure to use escape the data if you're going to put it in your database.

Unable to print error message in foreach in magento admin

Hi i have added a new mas action in the sales order grid which allow create batch invoices.
For this my controler file is
<?php
class Iclp_Batchupdate_IndexController extends Mage_Adminhtml_Controller_Action
public function batchinvoiceAction ()
{
$already = " already ";
$refererUrl = $this->getRequest()->getServer('HTTP_REFERER');
$this->loadLayout();
$this->renderLayout();
$orderIds = explode(",",$this->getRequest()->getParam('order_ids'));
foreach ($orderIds as $orderIdss) {
$order = Mage::getModel('sales/order')->load($orderIdss);
//echo $orderIdss ."<br/>";
//echo "already ".$order->getStatusLabel();
try
{
if(!$order->canInvoice())
{
echo Mage::getSingleton('core/session')->addError($orderIdss.$already.$order->getStatusLabel());
}
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
if (!$invoice->getTotalQty()) {
Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
}
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();
$transactionSave = Mage::getModel('core/resource_transaction')->addObject($invoice)->addObject($invoice->getOrder());
$transactionSave->save();
$order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save();
//echo "Invoice are created";
}
catch (Mage_Core_Exception $e) {
}
}
//A Success Message
Mage::getSingleton('core/session')->addSuccess("Some success message");
//A Error Message
Mage::getSingleton('core/session')->addError("Some error message");
//A Info Message (See link below)
Mage::getSingleton('core/session')->addNotice("This is just a FYI message...");
//These lines are required to get it to work
session_write_close();
$this->getResponse()->setRedirect($refererUrl);
}
}
every thing is working fine but the problem is it is not printing the error message in foreach in above code
if(!$order->canInvoice())
{
echo Mage::getSingleton('core/session')->addError($orderIdss.$already.$order->getStatusLabel());
}
but the bottom error message are displayed properly. MOreover if i extend the class with front-action than it also prints the foreach messages. Please suggest where i am doing the mistake
You should add your errors and messages to admintml/session and not to core/session when you are in adminhtml. That should display the message correctly. You shouldn't need session_write_close();. There is also no need to echo the message, that should be handled automatically by Magento after the redirect.
There is also no need to call $this->loadLayout(); and $this->renderLayout(); because you are redirecting at the end.
Finally, regarding the redirect, you should not read the referrer yourself, Magento can to that for you more reliably. Just use the $this->_redirectReferer(); method instead of $this->getResponse()->setRedirect($refererUrl);.

Get current url and create share link (yahoo messenger)

I have this function to get the current page url:
<?php
function currentPageURL() {
$curpageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$curpageURL.= "s";}
$curpageURL.= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$curpageURL.= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$curpageURL.= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $curpageURL;
}
echo currentPageURL();
?>
And I need to create a link to share the page's URL by a href.
I tried:
Link
But I'm not getting the currentPageURL; it's blank.
Just stumbled on this one. Seems like you're using a variable and not the function call in your href. This should work:
Link

Trigger.io PHP session expires

folks!
I'm trying to run an HTML app in Trigger.io. This app calls ajax to load some data from a PHP page. At this moment, I authenticate the user and start the session. After this, I have to call another PHP page. So, I check for the session started, and I found that the session is not active anymore. The second call is made right after the first one.
This happens when I try to run the app from the Trigger.io ToolKit, using iOS Simulator ( I'm using a Mac - OS X Mountain Lion ). When I test the same app in the Safari, it works perfectly: my PHP server recognizes the session started earlier, and the second page is loaded by ajax.
Is there any parameter I have to set? Or Trigger.io does not support PHP sessions?
Thank you.
Marcio
You should use forge.request.ajax(params) to make ajax requests, this is because of cross domain restrictions, as Forge apps are loaded as file:// urls on iOS.
forge.ajax.request takes a similar input to jQuery's $.ajax so you can easily switch between them if you want to use the same code on a website as well as in your Forge app.
More documentation is available in our docs: http://docs.trigger.io/en/v1.4/modules/request.html#ajax
#Connorhd , thank you so much for helping!
First of all, I do an ajax request to "app_authUser.php" to authenticate user. Then, if Ok, I do another ajax request to "app_loadFeed.php" to load data from server.
In the first ajax request, I pass, in the post parameters, the user data ( login and password ) to authenticate the user on database. The first command I call on the php file "app_authUser.php" is "session_start()", as follow:
CODE FOR "app_authUser.php" ON THE SERVER SIDE:
<?php
session_start();
// Just for test;
$idSession = session_id();
echo $idSession;
/*
Test user authenticate. If ok, then I assign idUser to $_SESSION['idUser'] variable.
*/
if( $loginOk == true ){
$_SESSION[ 'idUser' ] = $User->idUser;
}
?>
In the code above, the user is authenticated and $_SESSION['idUser'] is correctly initiated. In the second ajax request, I test if the variable $_SESSION[ 'idUser' ] is set. That's the point: it isn't set anymore. Again, I call "session_start()" at first:
CODE FOR "app_loadFeed.php" ON THE SERVER SIDE:
<?php
session_start();
// Just for test;
$idSession = session_id();
echo $idSession;
if( isset( $_SESSION[ 'idUser' ] ) ){
/* Load data from database to return to user.... */
echo ...
}
else{
echo '0';
}
?>
The test above always return false when I call my app from Trigger.io compiler. But, when I call my app from Safari, it always returns true.
I found out that the function "session_id()" returns different values in both ajax requests when the call is made from app running on Trigger.io compiler, but the same value when app is running on Sarari:
Example:
Requests from Trigger.io:
First request: echo $idSession returns "m7dbsv7qqem92os39lv5ao2ta1"
Second request: echo $idSession returns "h49pble06n7ao9pum06kt4dph0"
Requests from Safari:
First request: echo $idSession returns "2cbhin1185fm5ehvbb15k6n0b1"
Second request: echo $idSession returns "2cbhin1185fm5ehvbb15k6n0b1"
That means the session isn't the same at the first example. Why does this happen?
I'm not using forge.request.ajax in my app. That's my ajax code, in my javascript:
<script>
function openAjax()
{
try
{
var ajax = new XMLHttpRequest();
}
catch(e)
{
try
{
var ajax = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(ee)
{
try
{
var ajax = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(eee)
{
var ajax = false;
alert("Seu navegador não suporta AJAX!")
}
}
}
return ajax;
}
// This is the function called at second time, just after I authenticate my user and have session opened.
function loadFeed(){
var url = CT_URLBase + 'app_loadFeed.php'; // CT_URLBase = 'http://192.168.1.100/' in my local environment.
var parameters = '';
var ajax = openAjax();
ajax.open('POST', url, true);
ajax.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=iso-8859-1');
ajax.onreadystatechange = function(){
if (ajax.readyState == 4){
if (ajax.status == 200){
var retorno = ajax.responseText;
if( retorno != '0' ){
/* Return OK!!! */
}
else{
/* Error: return is 0 (zero), that means session is not started.
}
}
}
}
ajax.send(parameters);
}
</script>
What most makes me in a misunderstanding is that the same code works perfectly in Safari, but not in Trigger.io.
By the way, I visited your website, connorhd.co.uk ! Great job! It's a great place to find "Interesting stuff"! Congratulations!
Thank you so much for your help!!!
Marcio Clume

Resources