PDA

View Full Version : how do I use this php redirect on my pages?



ajfmrf
08-31-2012, 05:08 PM
<?php
$referrer = $_SERVER['HTTP_REFERER'];
if (preg_match("/site1.com/",$referrer)) {
header('Location: http://www.customercare.com/page-site1.html');
} elseif (preg_match("/site2.com/",$referrer)) {
header('Location: http://www.customercare.com/page-site2.html');
} else {
header('Location: http://www.customercare.com/home-page.html');
};
?>

The guestbook/comments script is here:
http://www.web-user.info/enlighten/guestbook.php

I put it on this page here:
http://www.web-user.info/enlighten/index.php

But when a person clicks on the post button you are taken to the guestbook page.When you use the back button,you see it posted your comment but took you away to the other page.

CAn the above be made to redirect a person if they go to that comments page from the index page?

ajfmrf
08-31-2012, 05:19 PM
Would I put this on the guestbook.php page?


<?php
$referrer = $_SERVER['HTTP_REFERER'];
if (preg_match("/index.php/",$referrer)) {
header('Location: http://web-user.info/enlighten/index.php');
} else {
header('Location: http://web-user.info/enlighten/index.php');
};
?>

so if it comes from my index.php to that guestbook page it will automaticly go back to the page you were on(index.php)

ajfmrf
09-02-2012, 12:15 AM
Anyone?

Do I have it right or wrong for what I want to do?

djr33
09-02-2012, 12:26 AM
I don't understand exactly what you want to do. The referrer isn't necessarily going to be accurate, but unless the user is intentionally tricking it (or the browser just doesn't send it!) it should be accurate.

But this code will do exactly the same thing regardless of what the referrer was. Is that intentional?

<?php
$referrer = $_SERVER['HTTP_REFERER'];
if (preg_match("/index.php/",$referrer)) {
header('Location: http://web-user.info/enlighten/index.php');
} else {
header('Location: http://web-user.info/enlighten/index.php');
};
?>

Do you want users to be able to visit the page if they choose to? Or should it always redirect them? If so, just use a redirect:

<?php
header('Location: http://web-user.info/enlighten/index.php');
?>


Additionally: you do not need a semicolon to end an if statement's {}. So it's just if () {}, not if () {};.

ajfmrf
09-02-2012, 01:17 AM
Hi Daniel,this page http://www.web-user.info/enlighten/index.php ,has the guestbook added by way of an php include like this
<php>
<?php include("guestbook.php"); ?>
</php>

My problem is that when you add a post and click on the 'post' button it takes you to the guestbook.php page and you have to
use your back button to go back to the enlighten page you were at.

I am trying to find a way for that to not happen and you stay on the enlighten.php page without leaving it and having to go back to it.

I thought that using the previous script it would automaticly take people back to the enlighten.php but I don't know if that
is the best way to do this or not.

AS you might have noticed,sometimes things just don't work out right for me and adding something makes a bigger more difficult issue for
me to figure out.

Thankfully I can usually get help here where many of you are more versed with php,css,javascript and more sometimes-lol

Anyway,adding the comment script(guestbook.php will eventually be renamed to comments.php and I will update that site with my humble
muses and /or thoughts

djr33
09-02-2012, 01:35 AM
The redirect will work, but it will mean that no one can intentionally access that page either.

Another way to do this that would be more reliable (rather than hoping that the referrer information is correct) would be to check if a certain value is $_POST is set.

Try this:
1) Add the following code to the very top of your comments page:
<?php print_r($_POST); exit; ?>
2) Submit a comment (the contents are irrelevant and won't be stored). Note: the page won't work any more, but that's temporary-- don't worry. ("exit;" ends execution of the script.)
3) Look at the output of the $_POST array. Now, see which items were submitted and probably wouldn't exist if that page hadn't been submitted. If the only reason to use a form to submit to that page would be to submit this form, then it doesn't matter which one you pick. But it might be a little more reliable to pick something like $_POST['comment'] or another field that is probably only going to be found in your comments form.
4) Remove that PHP code from the page and it will work again.
5) Now, instead of using the referrer, use an if statement that checks if that value was submitted. Assuming 'comments', it would be the following code:


<?php
if (isset($_POST['comment'])) {
header('Location: http://web-user.info/enlighten/index.php');
}
?>

Note: doing it this way will not allow users to stay on the comments.php page regardless of where they submitted from. So if you did want to allow them to post from the comments.php page, then you might want to rethink it.


The solution would be to modify the comments script directly, but I don't know if you're comfortable doing that. An easy way is to include a form value in the script that then lets you know where to redirect them. But that can be a little tricky and would require editing the comment form.

ajfmrf
09-02-2012, 02:23 AM
I would be willing to try anything but I would have to have help due to my limited Knowledge.

It just figures that it is not an easy fix-my luck-lol

If you have time,I would greatly appreciate the help.Or if not maybe someone else could help me.

I intend on using this enlighten page to be a place I can present my opinion of things going on around home and anywhere else.

Here is a text file of the guestbook
http://www.web-user.info/enlighten/guestbook.txt

djr33
09-02-2012, 03:54 AM
Simple question: do you intend to allow users to ever use (=see) the comments page?
If not, we can fix this and effectively remove the comments.php page from your system. (Using a redirect is easiest.)

ajfmrf
09-02-2012, 04:39 AM
No, I want the comments to be viewable for anyone.I can delete it if I don't want it there for some reason.

But I think it would be cool fro anyone to see what others are posting.I want to use this like what you see all over the place online.I was working on using captcha to prevent security issues but it did not work so I figured to get it working otherwise and then I would tackle that part.
Look at this page
http://espn.go.com/nfl/story/_/id/8326902/arizona-cardinals-trade-cb-aj-jefferson-minnesota-vikings-pick
down a little you can see 'ESPN Conversations'

There you can add your comments or see all of them.This is what I want to do.

I presume it is easier to ave them in view all the time verse having to click a button to add or view comments

djr33
09-02-2012, 06:28 AM
What I'm asking is specifically about the comments.php page, not your index.php page. Do you want visitors to be able to access both, or just to view the comments (=discussion) on the index page (=not the comments.php page)?

I really don't see the problem if you want the comments.php page to be visible. Why is it a problem that they are taken there when they post? If you want to permanently hide that page, you can do that with a redirect. But if you ever want them to be able to go there, why stop it from posting to that location?

ajfmrf
09-02-2012, 02:34 PM
Well,I just think it is odd to be taken to the comments.php -away from the enlighten page

djr33
09-02-2012, 08:21 PM
If they're supposed to be able to access the comments page, I think that's fairly normal-- they submit a comment and go to the comments page.

It sounds like this might just be a little too complicated with this script. There are so many comment scripts out there, you can probably find one that integrates better. This one is generally working, though, so maybe it's ok for now?

You can certainly try the redirect methods you had before, but I'm not sure they'd be 100% reliable, at least without modifying the form a bit to track where the user is coming from.

ajfmrf
09-03-2012, 03:02 AM
Okay,I will start to look for a replacement script Daniel-thanks for your help