Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

henock_barakael's avatar

Prevent someone from displaying the source code

How can I prevent someone from displaying the source code of the web page in browsers using laravel?

0 likes
6 replies
LaryAI's avatar
Level 58

It is not possible to completely prevent someone from viewing the source code of a web page, as it is ultimately downloaded and rendered on the client-side. However, there are some measures you can take to make it more difficult for someone to view the source code.

One approach is to obfuscate the code, which involves transforming the code into a form that is difficult for humans to read and understand. This can be done using tools like JavaScript obfuscators or minifiers.

Another approach is to use server-side rendering, which involves generating the HTML on the server and sending it to the client as a pre-rendered page. This can make it more difficult for someone to view the source code, as they would need to intercept the network traffic to see the HTML.

However, both of these approaches have limitations and trade-offs, and ultimately cannot completely prevent someone from viewing the source code if they are determined to do so.

// Example of using server-side rendering with Laravel
Route::get('/', function () {
    $data = [
        'title' => 'My Page',
        'content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
    ];
    return view('my-page', $data);
});

// my-page.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>{{ $title }}</title>
</head>
<body>
    <h1>{{ $title }}</h1>
    <p>{{ $content }}</p>
</body>
</html>
Tray2's avatar

You can't, it doesn't matter what you do, once the html is sent to the browser, the user has access to all you html, css and javascript.

henock_barakael's avatar

@Tray2 I saw for example in a site, when you click to see the source it gives this

<body><noscript><strong>We're sorry but FRESHPAY doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript>

That what i need

JussiMannisto's avatar

@henock_barakael That doesn't hide the source code from the browser, it's just a warning shown if the browser has JavaScript disabled. The browser must still receive the page source to render the page. You can see that data using your default browser tools.

The real question is: why do you want to try to hide your page code? It cannot help with security. Any data you send to your client is accessible to the user. If you're trying to hide sensitive information, this is not the way to do it.

Snapey's avatar

But that line does not do what you asked

Its purpose is to show a warning to the user if they have javascript disabled

AddWebContribution's avatar

you can not prevent the user to view the source code but

You can simply disable the right click by following

...

and you can use script for this

document.onmousedown = disableclick; status = "Right Click Disabled"; Function disableclick(e) { if(event.button == 2) { alert(status); return false; } }

Please or to participate in this conversation.