Can you fix my PHP If Statement, using a Href? - session

I am using an if statement and trying to get one of two statements to show, depending on the session running.
please help. thank you.
if (session_name== "admin"){
echo ("[Admin Logout]");
}else {
echo "[Member Logout]";
}
?>
Also there is 2 syntax errors on the echo parts.

You if you use double quotes around the argument to echo, you should use single quotes inside it for the HTML attribute, or vice versa (an alternative is to escape the inner quotes with backslash, but I never understand why people prefer this). And you need $ before the variable name.
if ($session_name== "admin"){
echo ("<a href='admin/logout.php'>[Admin Logout]</a>");
}else {
echo "<a href='logout.php'>[Member Logout]</a>";
}

Related

how to break line in laravel language files?

How to add line break in laravel language files?
I have tried to use ,, \n\r,
to break line and add new line but all these not work.
return [
'best_hospitality' => 'Simply <br /> the best hospitality',
];
You can use
'best_hospitality' => "<pre>Simply\r\nthe best hospitality</pre>",
or 'best_hospitality' => sprintf ('<pre>Simply%sthe best hospitality</pre>',PHP_EOL ),
please note the use of double quotes in the first example, it is not working with single quotes if you use the \r\n inside the string, this is why
if you try echo(Lang::get('message.best_hospitality')) you will see the new line:
I am not so sure if you need the pre tag, depends where you need to use the Lang for html or not, eg using (double quotes here):
'best_hospitality' => "Simply\r\nthe best hospitality",
and var_dump(Lang::get('message.best_hospitality')); exit;
has the output
C:\wamp64\www\test\app\Http\Controllers\TestController.php:24:string 'Simply
the best hospitality' (length=39)
Does this cover your case?
You need HTML Entity Names on your lang files.
Try this :
return [
'best_hospitality' => 'Simply <br> the best hospitality',
];
May I suggest creating a custom helper function for this specific case?
Add this into your helpers.php file:
if (! function_exists('trans_multiline')) {
/**
* Retrieve an escaped translated multiline string with <br> instead of newline characters.
*/
function trans_multiline($key, array $replace = [], string $locale = null): string
{
return nl2br(e(__($key, $replace, $locale)));
}
}
Now you will have a function trans_multiline() available for you in any view, which will behave pretty much like the built-in __() helper.
The function will fetch a localized string of text and replace any newline \r\n symbol with <br> tag.
Caveat: For a proper escaping it must be done before nl2br() function, like you see in the code above. So, to prevent any weird errors due to double-escaping, you must use this custom helper without any additional escaping, like so:
{!! trans_multiline('misc.warning', ['name' => 'Joe']) !!}
Escaping will be handled by the e() function (which is what Laravel uses under the hood of {{ }}) inside the helper itself.
And here's how you define a multiline translation string:
'warning' => "There's only 1 apple, :name!\r\nDon't eat it!"
Make sure to use double-quotes, so PHP actually replaces \r\n with a newline character.
Obviously, parameter replacing still works exactly like with the __() helper.

%s variable not working in Magento

I always use %s variable as a placeholder when there is error or success such as this code:
Mage::getSingleton('customer/session')->addError(Mage::helper('module')
->__('Hi %s, there is an error', $name));
The problem is: sometimes it works, sometimes it doesn't. Surely the $name does contain a valid string value (I've var_dump it many times). Do you know why?
As others have noted, you're not limited to one argument in your translated string. Some examples:
One argument, in one place:
Mage::helper('module')->__('Hi %s, there is an error', $name);
Two arguments, in two places:
Mage::helper('module')->__('Hi %s, there is an error with code %s', $name, $code);
One argument, in two places:
Mage::helper('module')->__('Hi %1$s, there is an error. Your name is %1$s', $name);
Two arguments, swapping places:
// Template or block file
Mage::helper('module')->__('Hi %1$s %2$s, there is an error', $firstname, $lastname);
// CSV translation
"Hi %1$s %2$s, there is an error", "%2$s, %1$s received an error"
Documentation on the PHP function being used to power this can be found here: vsprintf
(PS: The double-quoting that knase is talking about only applies to your CSV file, where a double-quote is an escaped single quote.)
%s varible work in translate magento. You can write in .csv translate file such as in example:
"Hi %s, there is an error","An error is %s"
Or if you use "%s" for translate you must write double slashe twiсe for shield. Look example:
"Hi ""%s"", there is an error.","An error is ""%s""."
You write for this translate:
Mage::helper('module')->__('Hi "%s", there is an error', $name);
What version of Magento are you using? In current version of Magento, the variable replacement happens here
#File: app/code/core/Mage/Core/Model/Translate.php
...
//array_unshift($args, $translated);
//$result = #call_user_func_array('sprintf', $args);
$result = #vsprintf($translated, $args);
...
I imagine dropping some debugging statement in there could help lead to a better understanding of the problem.
Make sure in the csv file you use double quotes " instead of single quotes '.
Incorrect:
'Welcome %s', 'Hello %s'
Correct:
"Welcome %s", "Hello %s"
Wow, it seems if there is more than one %s, the translation won't work. So it must be only one %s for the translation to work properly.

Error when wrapping code in "while" loop

OK, I am a little embarrassed but I gotta ask anyway. I have a perfectly-running script that I wrapped in an infinite loop with a sleep at the end:
while($true){}
That's all I added was this code and all of the curly braces match so why am I getting this error:
"Missing closing statement block in line x, where x is the last line in the file"
function A(){
}
while($true){
[... Do Stuff ...]
Start-Sleep -s 10
}
I remove the encapuslating while{} and all is fine.
Any ideas?
Well, the syntax posted is correct.
My guess is you retyped this into the browser, I would check all your ( ) to make sure you dont have { } in there by mistake.
Otherwise, need to copy and paste your actual code.

var_dump says String of 75 characters, can't echo it and doesn't json_decode()

I am posting some data to a PHP page with AJAX, but I can't decode it. json_decode() returned NULL. So I figured that if I'd echo it I could run it through jsonlint. But nothing showed up. To find out if the data reaches the PHP page I tried to var_dump() the data. Strange enough it showed:
string(75) "{\"title\":\"fds\",\"body\":\"fds\",\"tags\":\"fds\",\"type\":\"question\"}"
So the data reaches the page. I ran it through jsonlint after removing the backslashes and it said the JSON was valid. How is this possible?
I got a valid JSON string of 75 characters according to var_dump() and jsonlint, but I can't echo it or json_decode() it.
EDIT:
Apparently I did something wrong, I can echo it now (I probably made a typo in the $_POST array key). It echoes this:
{\"title\":\"tre\",\"body\":\"tre\",\"tags\":\"tre\",\"type\":\"question\"}
Once again, without backslashes JSONLint tells me it is valid JSON.
EDIT 2:
Here's the PHP code:
// code to check if the user is logged in
$post = json_decode($_POST['q']);
echo "var_dump(\$_POST['q']): ";
var_dump($_POST['q']);
echo "<BR /><BR />";
echo "var_dump(\$post): ";
var_dump($post);
Ouput:
var_dump($_POST['q']): string(74) "{\"title\":\"gfd\",\"body\":\"gf\",\"tags\":\"gfd\",\"type\":\"question\"}"
var_dump($post): NULL
JS code:
var simpleObj = {title: "rew", body: "rew", tags: "rew", type: "question"};
$.post('savepost.php', "q=" + JSON.stringify(simpleObj), function(data) {
$('#resultDiv').html("DATA: " + data);
});
Final edit:
I finally solved it myself. I actually discovered the problem even before I asked, but thought it was 'harmless' and didn't think that could cause this problem.
I found out the answer! When I ran it through jsonlint.com it said it was invalid because of the backslashes. I thought these were just there to mark the quotes in the JSON as normal quotes, not string ending and starting quotes and I thought they were 'harmless'.
I then thought of the function addslashes(), and figured there should be a reversed one too. I found stripslashes() and that solved the problem.
New code:
// code to check if the user is logged in
$post = json_decode(stripslashes($_POST['q']));
echo "var_dump(\$_POST['q']): ";
var_dump($_POST['q']);
echo "<BR /><BR />";
echo "var_dump(\$post): ";
var_dump($post);

Access array variable in session (CodeIgniter)

I have an array called config. I'm trying to echo a variable from the array in the session.
I've tried:
echo $this->session->userdata('config['item']');
but it doesn't work. What's wrong with my syntax here? I've print_r'd my session and the items are in the config array. I've also tried:
echo $this->session->userdata("config['item']");
I get no errors this time, but no data either.
If config is an array . And item is string name of what you want to get from config then
echo $this->session->userdata($config['item']);
or
echo $_SESSION[$config['item']];
If config is an array inside session you should first get it.
$tmp = $this->session->userdata('config');
echo $tmp['item'];
or
echo $_SESSION['config']['item']
Sorry for my english.
If you want to use the session array, use the variable, not the function:
echo $this->session->userdata['user_data']['item'];
If you want to write:
$this->session->userdata['user_data']['item'] = 'value';
$this->session->userdata['other_data']['other'] = 'value2';
$this->session->sess_write();
This allows you to edit values in array just like you do with $_SESION['user_data']['avatar'] = $avatar, with 'only' one extra line and only using CI library.
Always escape your string it should be this way:
echo $this->session->userdata('config[\'item\']');

Resources