How to hide the source code on web pages in php?

Dani AI

Generated

Short answer: you cannot reliably hide the HTML/CSS/JavaScript that a browser receives. PHP runs on the server and its source is executed there; only the generated output (HTML/JSON/etc.) is sent to the client. The browser (and its developer tools or "view source") shows whatever was delivered to it, so any client-side markup or scripts are inspectable. (php.net)

Regarding the thread replies: the client-side tricks suggested by and (blocking right‑click / keys) only annoy casual users and are trivial to bypass — was correct to call that out. The older idea from to hide functionality in Flash is no longer a practical option; Flash reached end‑of‑life and browser support was removed. (stackoverflow.com)

Practical, secure approach (what actually protects code and secrets):

  • Keep sensitive logic and credentials on the server. Expose only the minimum data needed by the client via authenticated APIs.
  • Always validate and filter data on the backend; never rely on client-side filtering for security. (owasp.org)

Example PHP pattern (move work to server; return only safe data):

<?php
// config stored outside webroot or in env vars
$pdo = new PDO($_ENV['DB_DSN'], $_ENV['DB_USER'], $_ENV['DB_PASS'], [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

$stmt = $pdo->prepare('SELECT title, body FROM articles WHERE id = ?');
$stmt->execute([$id]);
header('Content-Type: application/json; charset=utf-8');
echo json_encode($stmt->fetch(PDO::FETCH_ASSOC));

If the goal is deterrence rather than absolute secrecy, minify/obfuscate JS or compile performance/IP‑sensitive parts to WebAssembly — these raise the bar but do not make client code secret. Final checklist: use HTTPS, store secrets outside webroot or in environment variables, run PHP correctly (so files are executed, not served as text), return only necessary fields from APIs, and enforce server-side access controls.

Recommended Answers

All 6 Replies

hi gurusubramani, Use the following javascript code in your index.php page and it will used to disable the mouse right click option.

    <script language=JavaScript>
        var message="Function Disabled!";
        function clickButton()
        {
            if (document.layers||document.getElementById&&!document.all)
            {
                alert("Permission Restricted");
                return false;
            }
        }
        if (document.layers)
        {     
            document.onmousedown=clickMF;
        }
        else  
        {
            document.oncontextmenu=new Function("return false")
        }
    </script>
  function clickButton()
    {
        if (document.layers||document.getElementById&&!document.all)
        {
            alert("Permission Restricted");
                return false;
        }
    }
    if (document.layers)
    {     
        document.onmousedown=clickMF;

    }
    else  
        {
        document.oncontextmenu=new Function("return false")

        }

try this code......mr.subramani

Even if you disable right click they can still access it through f12 button

Thank you Rajalakshmi and Palani. Your code is very useful to us.

if you want to hide data you can hide it within flash, html/javascript is always available to view - the right click disabling just annoys me, to put it politely. Means i have to go to the menu instead of right clicking - such an effort.

Will prevent less technical people though

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.