Jack Jack - 1 month ago 6
PHP Question

Javscript call php header

Hello i have the following javascript code:

function like(){
if(loged){
if(liked){
likes-=1;
liked = false;
document.getElementById('alike').src = "img/icons/like.png";
}else if(disliked){
dislikes-=1;
disliked = false;
likes+=1;
liked = true;
document.getElementById('alike').src = "img/icons/like1.png";
document.getElementById('adislike').src = "img/icons/dislike.png";
}else{
likes+=1;
liked = true;
document.getElementById('alike').src = "img/icons/like1.png";
}
}else{
<?php header("Location:login");?>
}

}


I want to redirect the user to the login page how can i user php inside javascript to this. I do not want to have to use any other things like ajax etc. Thanks.

Answer

You do not need to use PHP header() to redirect with JS. You can simply use the window.location property:

window.location.href = "your_url";

//or

window.location = "your_url";

//or

location.href = "your_url";

//or

location = "your_url";