1

Hi i want to stop user changing my scripts from console of browser.

For example: In html,if i have div like below

<div id="demo">
Hi..
</div>

and jQuery

$('#demo').hide();

But still user can do inspect element of this page and they change script or coding from console and they can view the div which has ID demo.So please tell me how to stop user changing my code or even though if they change it should not show the div.

Raja
  • 41
  • 3
  • You could minify your code which would make it practically unreadable for users. In [this topic](http://stackoverflow.com/questions/1678471/how-to-minify-jquery-files) are several suggestions given. – Matheno Feb 05 '16 at 07:47
  • Only slightly, check out http://www.cleancss.com/html-beautify/ and paste in some minified code! Basically you cannot prevent stuff like that - anything on the client side can be manipulated to a degree - you can enforce checks server side perhaps – Professor Abronsius Feb 05 '16 at 07:55

3 Answers3

2

Once your page is sent to user, you can't prevent him to do anything. Even with a minified js, he'll be able to force a display on the element. All you can do is hiding it php side before sending page

For exemple :

<?php
session_start();
if (!empty($_GET['demo']) && $_GET['demo'] == "whatever") $_SESSION['show_demo'] = true;
?>

<?php if(isset($_SESSION['show_demo']) && $_SESSION['show_demo'] == true): ?>
<div id="demo">
    Hello World
</div>
<?php endif;?>
<a href="?demo=whatever">Click here to show demo</a>
Gwendal
  • 1,273
  • 7
  • 17
1

Here you could read an article with some useful words: How to hide a JS Code

Also, you could use an obfuscation

You just need to realise that you can't hide a JS code from user, because browser should download it and execute. If you want to encrypt it you should send a pass to browser which user'll get too.

Just imagine: I'm telling you 1 and you should tell me the same. What would you tell me if I'd told you 2 ?

And ... here it is ... problem of all programmers. All programmers want hide their own code : ). Just ... believe me. It is.

One more suggestion is just to try this: Online Obfuscator

If you want to do this with a PHP code, try this:

// action.php
<?php 

if ( !isset($_POST['show']) ) {
    die('HACKER !');
} else {
    echo '
    <html>
    <head>
    <title>Main Page</title>
    <body>
    <div id="demo">DEMO</div>
    <br>
    <form action="action.php">
    <input name="show" style="display: none;">
    <input type="submit" value="Try me !"/>
    </form>
    </body>
    </html>';
}
?>

These files should be in same folder.

// index.html
<html>
<head>
<title>Main Page</title>
</head>
<body>
<form action="action.php">
<input name="show" style="display: none;">
<input type="submit" value="Try me !"/>
</form>
</body>
</html>

Good luck : )

Artfaith
  • 1,183
  • 4
  • 19
  • 29
  • Thank you for sharing your thoughts – Raja Feb 05 '16 at 08:06
  • 2
    Not all programmers wan't to hide their code – DarkBee Feb 05 '16 at 08:08
  • Nah. If it's pretty as ... then yeah and if you want show it to other programmers who'll learn smth from it then yeah, too ... but if it's own and theres something very important - no. – Artfaith Feb 05 '16 at 08:11
  • Such a bold statement. What about all the opensource projects? Anyway thats another discussion but I don't think it should be in the answer – DarkBee Feb 05 '16 at 08:14
1

I was recently doing research on this subject. It might not be the optimal solution but php has a built in function called htmlspecialchars

See this for further explanation:

http://php.net/manual/en/function.htmlspecialchars.php

Offcourse your tag says php, but i don't see any php code at the moment in your current code. What i can Also recommend is these 2 links for achieving the same effect with different programming languages:

http://www.toao.net/32-my-htmlspecialchars-function-for-javascript

Hope you achieve your goal further. Good luck

izk
  • 234
  • 6
  • 19