MsKrissy
asked on
Header Location: Parent
I have the following code:
session_start();
if(!isset($_SESSION['id'])){
	header("location:../login.php");
}else{
$id = $_SESSION['id'];
}
I can't get this to happen on the parent level. Can someone help me please.
ASKER
The reason I need it to happen on the parent level is because I am using an iframe, I could use a div,but I would still be having the same problem.
Is there anyway to incorporate the parent clause into either of your suggestions?
Is there anyway to incorporate the parent clause into either of your suggestions?
You can use JavaScript to access the parent. You could echo out javascript in your PHP. so your parent page has this:
function callme( url ) {
document.location = url;
}
and in your php script, call the javascript as
<script >
parent.callme('mypage2.htm l' );
</script>
Hope this helps
function callme( url ) {
document.location = url;
}
and in your php script, call the javascript as
<script >
parent.callme('mypage2.htm
</script>
Hope this helps
No idea if this implies to your code also, you can try with the target in the iframe to point to the top frame.
Something like
<iframe target="_top" src="...." >
Since as i said this will make all the responses to be directed to the top page.No idea how is your application working on this.
Something like
<iframe target="_top" src="...." >
Since as i said this will make all the responses to be directed to the top page.No idea how is your application working on this.
@shinuq document.location is not the one to use (deprecated and supposedly read-only)
window.location =
or perhaps in this case
parent.location =
is better
window.location =
or perhaps in this case
parent.location =
is better
Are you trying to see if a client is logged in? If so this design pattern may be helpful.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html
greetings, MsKrissy, you use a "relative" path to the login.php page in your header location call. . In my view, you should ALWAYS use the complete web address in any header location call, because your directory structure and page placement in your directories can change as your site grows or is reformatted to have added or removed presentations. The login.php and main index.php usually do not ever move. You might consider using the full web path like -
header('Location: http://www.yoursite.com/login.php');
header('Location: http://www.yoursite.com/login.php');
ASKER
Thank you all for your comments, but I have gotten more confused. Which means I probably didn't explain properly.
I have a page, that has a frame in it. When the user tries to access that frame through a button click, it checks to see if the user is logged in or not. If they are not logged in, they get redirected to the login page. The problem is when it redirects the user, it tries to load the login page in the frame instead of the parent level.
I see many different ways to handle an issue to redirect, but I don't see one that checks the users session_id first. I read that this is something that needs to be done in javascript since PHP does not allow you to parent the location. Here is what I have so far.
session_start();
if(!isset($_SESSION['id']) ){
header("location:login.php ");
}else{
$id = $_SESSION['id'];
}
I have a page, that has a frame in it. When the user tries to access that frame through a button click, it checks to see if the user is logged in or not. If they are not logged in, they get redirected to the login page. The problem is when it redirects the user, it tries to load the login page in the frame instead of the parent level.
I see many different ways to handle an issue to redirect, but I don't see one that checks the users session_id first. I read that this is something that needs to be done in javascript since PHP does not allow you to parent the location. Here is what I have so far.
session_start();
if(!isset($_SESSION['id'])
header("location:login.php
}else{
$id = $_SESSION['id'];
}
Return a script:
top.location.replace("logi n.PHP")
top.location.replace("logi
if(!isset($_SESSION['id'])){
exit('<script type="text/javascript">parent.location.href = ".$sProtocol."://".$_SERVER['HTTP_HOST'].'/login.php"</script>');
}else{
$id = $_SESSION['id'];
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
I got it to work using the following,it is based on your responses PatrickAdrichem and mplungjan. thank you both. Even though I got it working, I wanted to pass it along for your input.
if(!isset($_SESSION['id'])){
exit('<script type="text/javascript">parent.location.href = "../login.php";</script>');
}else{
$id = $_SESSION['id'];
}
The scenarios you both gave me had a link error.
I did not validate the PHP part of the script given but I still suggest you use location.replace Â
ASKER
Thank you all for your help
Open in new window
however if this does not work, you can always add the domain to the location
Open in new window