관련링크 2번째에 랜덤으로 링크 뿌려주기
관련링크
본문
setup.php 는 아래처럼
link_1 = "https://link1.com";
link_2 = "https://link2.com";
link_3 = "https://link3.com";
link_4 = "https://link4.com";
link_5 = "https://link5.com";
link_6 = "https://link6.com";
link_7 = "https://link7.com";
link_8 = "https://link8.com";
link_9= "https://link9.com";
link_10 = "https://link10.com";
A사이트에 올려두고
B사이트에서 A사이트의 주소를 랜덤으로 관련 링크2에 넣어줄수 있는 방법이 있을까요? 새로고침 할때마다 주소는 랜덤으로 계속 바뀌게요.
1번 사이트에 링크주소를 여려개 올린다.
2번 사이트에서 1번사이트에 링크를 랜덤하게 가져와 관련링크 2번째 란에 넣어준다.
링크값은 새로고침 할때마다 랜덤하게 바뀐다.
https://joytv77.store/newmv/%EC%84%9C%EC%9A%B8%EC%9D%98-%EB%B4%84/
(예시 사이트 입니다)
답변 4
curl을 이용해서 링크를 가져오는 것이 정상절차인데 자바스크립트를 이용해도 됩니다
setup.php
<?php
$linkarr=["https://link1.com",
"https://link2.com",
"https://link3.com",
"https://link4.com",
"https://link5.com",
"https://link6.com",
"https://link7.com",
"https://link8.com",
"https://link9.com",
"https://link10.com"];
shuffle($linkarr);
echo "link ='{$linkarr[0]}'";
===============view에서
<?php
//링크
$cnt = 0;
$view['link'][2] = "link2"; $view['link_href'][2] = "link2"; $view['link_hit'][2] =0;//----------추가
for ($i=1; $i<=count($view['link']); $i++) {
?>
<div class="d-flex my-1">
<div class="px-0">
<i class="fa fa-link" aria-hidden="true"></i>
</div>
<div class="pl-3 flex-grow-1 text-break-all">
<a href="<?php echo $view['link_href'][$i] ?>" target="_blank" id="link<?php echo $i?>"> <------- id 추가
<?php echo get_text($view['link'][$i]) ?>
<?php if($view['link_hit'][$i]) { ?>
--------------하단 자바스크립트
<script src="http://사이트.com/setup.php"></script>
<script>
$("#link2").attr("href", link);
$("#link2").text(link)
</script>
A-site setup.php
<?php
$wrap = [
'links' => [
"https://link1.com",
"https://link2.com",
"https://link3.com",
"https://link4.com",
"https://link5.com",
"https://link6.com",
"https://link7.com",
"https://link8.com",
"https://link9.com",
"https://link10.com",
]
];
echo json_encode($wrap);
?>
B-site
<?php
$content = file_get_contents('http://A-site.com/setup.php');
if ($content !== false) {
$data = json_decode($content, true);
$links = $data['links'];
$link = mt_rand(0, count($links) - 1);
echo $links[$link];
}
?>
저렴한 웹호스팅 중에는 php 의 file_get_contents 나 curl 을 허용해 주지 않는 데가 꽤 있습니다.
그러니 사이트를 랜덤으로 안전하게 튕기려면
프론트에서 자바스크립트로 튕기는 것이 코드는 길지만 더 효과가 확실합니다.
즉 저 변수들을 js 파일안에 심어 놓고 불러와야 안전하지 php 파일에 심어 놓으면 cutl 등이 막혀 있으면 당연히 안 될 것입니다.