Checking if a user is logged in with PHP and javascript - javascript

I am using a plugin userfrosting to manage users.
The PHP side of the app checks if the user is currently logged in.
The javascript side of the app checks what the permission of the user is.
EG.
<?php
if (isUserLoggedIn()){
?>
<script>
loadCurrentUserPermission();
</script>
<?php
}
?>
Now that's fine and dandy, but now I want to implement some user access restrictions in my PHP files. Can I use such a method inside of a php function? Because I don't want to escape the PHP and check for every action.
The next option would be to store it in a variable, but how can I escape a variable assignment midway to the outcome of a < script > ?
$userPermission;
if (isUserLoggedIn()){
$userPermission = (
?>
<script type="text/javascript">
userLoadPermissions();
</script>
<?php
);
}
?>
Finally what if I use jquery to store the permission level in a PHP session ID? is that safe? Can someone easily modify their own level?

What's wrong with echoing the js?
<?php
if (isUserLoggedIn()){
echo "<script>loadCurrentUserPermission();</script>";
// more php
?>
Edit: Oh, I see. Javascript, being server-side, will be loaded after the PHP, so it's not simple (or even possible) to have PHP, then JS, then more PHP execute on the same page. You could consider calling the rest of the PHP from somewhere else using an AJAX request but that might not be practical for all of your pages.

Related

Check if magento shopping cart is empty using javascript?

So I need to check if the cart on my magento site is empty or not.
I can do this using php like this:
<?php
$count = $this->helper('checkout/cart')->getSummaryCount();
if($count==0){
echo 'Order';
}
else {
echo 'CHECKOUT';
}
?>
However the problem is that using php it will get cached. So in order for the button to change you'd have to refresh the cache.
So I was wondering if there is any way to do this check in javascript instead of php as I can't think of a way to do it?
You could use AJAX to run a asynchronus php request (should be uncached) to get the state of your cart. With the result of that request you can work in javascript to change the state of your button.
You can find more info about ajax and how to use it here:
How does AJAX work?
Ajax tutorial for post and get
https://www.atwix.com/magento/ajax-requests-in-magento/

How to pass javaScript values to PHP

How to pass javaScript values to PHP, my code is following
<script>
function a{
var b = a;
}
</script>
<button onclick="a(2)">Values</button>
<?php
$select = mysql_query("select * from tabl1 where id='values'"); // values comes here
echo $select;
?>
There's a lot of thing you could do.
Principal things you have to know is that javaScript run on the client side (browser), while PHP is running on the server.
Then If you want to pass a variable from your JS to your PHP you have to make a server call.
There's various way you can use in order to send variable from client to server.
As I understand from your example, it looks like your php code and your javascript on the same file. so maybe call your file another time will be enough for you.
Let's say your file's name is index.php.
<script>
function yourJavascriptFunction(id) {
window.location.href = "index.php?id=" + id;
}
</script>
Then in change your PHP code to this:
<?php
$select = mysql_query("select * from tabl1 where id='".$_GET['id']."'"); // values comes here
echo $select;
?>
$_GET will get the variable you've sent in your Js function.
Doing like this will refresh the page.
May be you don't want to refresh the page? Then look at the ajax way.
I hope it helps you

Appending parameter to existing URL cause infinite loop

I have a page index.php, I want to append a parameter to the end of URL upon a certain action by user. So the result would be index.php?param=1
Using the below PHP code causing an infinite redirect loop.
<?php
header("Location: index.php?param=1");
?>
Using the below Javascript code result in infinite refresh loop.
window.location = "?param=1";
How can I append that parameter only once without encountering any loop using either PHP, Javascript or jQuery?
For the PHP version, try:
<?php
if( !isset( $_GET['param'] ) ){
header('location: index.php?param=1' );
}
?>
Please try add host name and then try.
Example - If you are using localhost.
<?php
header("Location: http://localhost/index.php?param=1");
?>
Apart from using Apache's mod_rewrite, you could check if the additional parameter is already set, e.g.
<?php
if (isset($_SERVER['QUERY_STRING']))
header("Location: index.php?param=1");
?>
It is better to use .htaccess files to set redirects due to there is the good practice to store logic and server configuration separately.
Here is .htaccess code for you:
Redirect /path/to/index.php http://yourdomain.com/path/to/index.php?param=1
or if you need to have permanent redirect use
Redirect 301 /path/to/index.php http://yourdomain.com/path/to/index.php?param=1
If you do not use Apache but Nginx or something else check their documentation how to make redirects. Typically it is very easy.

php and JavaScript zip code

What i'm trying to do is when a user enters a zip code in the input type i get the value of the input type store in origin_zipcode than add origin_zipcode inside the $url variable. From there it should run the xml and i should be able to display the city and state but it doesn't do that it im trying to make it work like
https://ship.onemorepallet.com/shipment/start?hsCtaTracking=296d76b9-a6fc-4140-83f9-de066c986716|9001d544-e760-4dae-ac72-36cc2922be55?utm_referrer=http%3A%2F%2Fwww.onemorepallet.com%2F
when you enter in the zip code
$("#origin_zipcode").keyup(function(){
var origin_zipcode = $("#origin_zipcode").val();
<?php
$url = "http://zipcodedistanceapi.redline13.com/rest/gNPseXsQyP3daTlKfRB3D0v0mPVC3jJj6zMlwec54fie8EZowAsaBXaXN5zQpjav/info.xml/origin_zipcode/degree";
$xml = simplexml_load_file($url);
$city = $xml->city;
$state = $xml->state;
?>
$(".labelzip").text(<?php echo $city,$state"; ?>);
});
You can't send a jQuery variable to a PHP script unless you refresh the page using one of a few possible methods. This is because while jQuery is client-side, php is server-side. After PHP has finished loading the page, it can't be used again without a page reload.
You can look at these questions for a possible solutions:
How to pass jQuery variables to PHP variable?
jquery 'variable' to php 'variable'
how to pass a jquery variable to php and then display it

whether to use php or javascript method for redirecting a page

Whenever I user php header("Location: index.php"), most of time then it does not work. It exactly DOES NOT work without any error nor any run-time fatal error. I don't use any echoing nor I keep empty lines prior to header() call. I double check everthing, but it does not work.
Now, I user JS solution. I write a function that gets the param, store it in an immediately created <input> field and the using Javascript in the same function, I get the value out of input and assign it to window.location.href= value; which then finalizes the redirection. Is my method reliable and good?
My function seems like this:
function redirect($address)
{
?>
<input id='this_address' value="<?php echo $address; ?>" />
window.location.href = document.getElementById("this_address").value;
<?php
}
Are you sure that your index.php is in the same directory as the scripts that calls redirect or vice versa...?
If not try header("location:../../index.php");
Setting and cookies or session variables will cause issues with a redirect. It has to be sent before any output or exchange with the server and the client. Check your browser log for any errors too. It might be helpful in determining why you are not achieving the result you want.
You could also implement both methods, but it would be more useful to find out why your PHP redirect is not effective.
If u can't able to find out the solution for php header prob, Just add the below code at the top of the page. This is not the perfect way, but we can use this...
ob_start();
Why not use Response.Redirect "../../index.php" to redirect to another page.

Categories

Resources