how to website only can access from iframe

Solution:

I think I know what you are trying to do, in which case I think this will work but I haven’t tested it (apply for other iframes and domains as you see fit).

site 2/3/4.com

if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] == 'http://domain1.com') {
    include_once('sidebar.php');
} else {
    header('Location: http://domain1.com');
}

site1.com

<iframe src="http://domain2.com" frameborder="0"></iframe>

I may have got the requirements wrong, but this should show your sidebar content if there is a referrer, and if not, it will redirect to domain1.com.

A more robust way (in case the referrer is not populated for some reason) might be to add a url parameter to the iframe src attribute, e.g.

<iframe src="http://domain2.com?iframe=1" frameborder="0"></iframe>

and then check for that

if (isset($_GET['iframe']) && $_GET['iframe'] == '1') {
    include_once('sidebar.php');
} else {
    header('Location: http://domain1.com');
}

but that is also easily manipulated, a better way may be to come up with a token/hash to use instead.