JavaScript to PHP

Hello everybody,
I know they are two different programing languages, but is it possible, lets say, translate PHP to JavaScript?
I’ve got this php code and i would do the same in JS.
Anybody understand me…?

<?php include("sesion.php"); ?>
Bienvenido:
<?php echo $_SESSION["usuario"]; ?>
<br /><br />
Estas en una p&aacute;gina segura con sesiones en PHP
<br /><br />
<a href="archivo-protegido2.php">Ir a otra p&aacute;gina segura</a>
<br /><br />
<a href="salir.php">SALIR</a>
1 Like

Well, you can’t really do that, since PHP is a server-side language incorporated into a frontend language while JavaScript does have a server-side part (NodeJS) and a frontend part, they can’t be used together like PHP.

1 Like

You kinda can but not directly.

Like @element1010 said as a client-side JavaScript application, you can’t directly use server-side sessions like you can in PHP.

But you can achieve something similar by using AJAX.

2 Likes

PHP is actually pretty similar to JavaScript, with vanilla, browser JavaScript you won’t be able to do any of the server-side actions PHP can, but with NodeJS you could.

Your example could easily be translated to JavaScript, it would look something like this:

Bienvenido:
<script>
	document.write(window.sessionStorage.getItem("usuario"));
	// although document.write is deprecated and I would recommend something more like this:
	const span = document.createElement("span");
	span.textContent = window.sessionStorage.getItem("usuario");
	document.body.append(span); // append is the same as appendChild
</script>
<br /><br />
Estas en una p&aacute;gina segura con sesiones en PHP
<br /><br />
<a href="archivo-protegido2.php">Ir a otra p&aacute;gina segura</a>
<br /><br />
<a href="salir.php">SALIR</a>