if elseif else preg_match windows location - php

I am creating a form where i want that if someone answer is a certain number like 2 or 7 the will be directed to diffrent pages.
I searched online and came up making this.
<?php
// define variables
$name "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!preg_match("/^3/",$name)) {
echo "<meta http-equiv="refresh" content="0; URL=http://google.nl/">";
} elseif (!preg_match("/^9/",$name)) {
echo "<meta http-equiv="refresh" content="0; URL=http://osm.nl/">";
}
else {
echo "<meta http-equiv="refresh" content="0; URL=http://facebook.nl/">";
}
}
?>
First i tested it by creating a validation form, which works fine. Trough research i edited the code. But now when i tried to open the page it is point blank.
How do i fix it that i can redirect the inputs

Personally I would Use Switch Statement and the php header function to redirect
In the file your posting the form to:
$page = $_POST['name'];
switch($page)
{
case 2:
header("Location: http://www.google.com");
break;
case 7:
header("Location: http://www.facebook.com");
break;
}
Should do the trick.

So here are all your errors:
Line 3 :$name "": Where is the "="?
Line 7, 9, 12: echo "<meta http-equiv="refresh": If you haven't figured that youself, consider using header as #NathanLeadill said.

Related

meta redirect with php inside

I have a php file. In it I want an error redirect like this:
<?php
$ponka = somerandomsite.com;
if (1=1)
{echo '<META HTTP-EQUIV="Refresh" Content="0; URL=http://$ponka/error.php">'}
?>
How can I achieve it?
You php contains lots of errors, here's a working sample
<?php
$site = $_GET['site'] // will get the variable $site form a url like script.php?site=somerandomsite.com
$ponka = "somerandomsite.com";
if ($site == $ponka){
echo "<META HTTP-EQUIV='Refresh' Content='0; URL=http://$ponka/error.php'>";
}
?>
Also, remeber that php as a function called header
<?php
$site = $_GET['site'] // will get the variable $site form a url like script.php?site=somerandomsite.com
$ponka = "somerandomsite.com";
if ($site == $ponka){
header("Location: http://$ponka/error.php");
}
?>

PHP redirect script

I want to redirect my site visitors form page B using my custom PHP script following this rule: If they come from page A, will be redirected to url 1, else i will display content/or url 2. I use this:
<?php
$referer = $_SERVER['HTTP_REFERER'];
$rest = substr("$referer", -8);
if($rest == "send.php")// if they come from my website page "send.php"
{
echo "<meta http-equiv=\"refresh\" content=\"0;url=http://google.com\">";
}
else
{
echo "$rest";
include 'content.php';
}
?>
.
Anyone can help?
<?php
$referer = $_SERVER['HTTP_REFERER'];
if (strpos($referer,'send.php') !== false)
{
echo "<meta http-equiv=\"refresh\" content=\"0;url=http://google.com\">";
}
else
{
echo "$rest";
include 'content.php';
}
?>
Instead of meta redirect you can also use header location.
header("Location:http://www.google.com");

Write text with echo() after reloading page with header()

I have page called account_settings.php and it's consist of change password, change profile pic, change user details (name, bio etc.). My question is how to write message with echo() after redirecting page with header().
Something like this:
if (true)
{
Do_Some_MySQL();
header("Location: account_settings.php");
echo "Success!";
}
else
{
echo "Error!";
}
Thank you for all replies. ;-)
You can't actually do something after sending a Location header - it is impossible.
Instead, you could use $_SESSION array value to perform your task. Like:
if (true)
{
Do_Some_MySQL();
$_SESSION['message'] = 'Error!';
header("Location: account_settings.php");
}
else
{
echo "Error!";
}
And then on your account_setting.php:
<?php echo $_SESSION['message'] ?>
This would be nice if the account_settings.php is not the same page as you currently are. Otherwise, you could use the following code:
if (true)
{
Do_Some_MySQL();
$error = 'Success!';
header("Location: account_settings.php");
}
else
{
$error = "Error!";
}
And on the same page:
<?php if($error) echo $error; ?>
Also don't forget to include session_start() on both pages if you didn't it yet.
I would use a SESSION variable:
on redirect-page:
<?php
#session_start();
if(true){
$_SESSION['success'] = 1;
header("Location: account-settings.php");
}
?>
and on account-settings.php:
<?php
#session_start();
if(isset($_SESSION['success'])){
echo "Success!";
unset($_SESSION['success']);
}
You cannot echo anything after you just redirected. The browser is already processing the request to redirect to another page, so it doesn't bother about displaying the message anymore. What you seem to be looking for is something called flash message. You can set a temporary message in the session and have it display on the new page. For example, in your account_settings.php page:
// Make sure you have an actual session
if (!session_id()) {
session_start();
}
if (true) {
Do_Some_MySQL();
$_SESSION['flashMessage'] = 'Success!';
header('Location: account_settings.php');
}
Then in your template file for account_settings, check if there is any flash message and display it accordingly (and unset it to avoid a loop):
if (isset($_SESSION['flashMessage'])) {
echo $_SESSION['flashMessage'];
unset($_SESSION['flashMessage']);
}
These people are correct...you can't send headers after a redirect. Although I think this would be a beneficial alternative. To send a GET request in your header and process it on the receiving page. They are suggesting to use $_SESSION vars, but you can use GET vars. Ex:
if (true)
{
//Do_Some_MySQL();
header("Location: account_settings.php?message=success");
//above has GET var message = Success
}
else
{
header("Location: account_settings.php?message=error");
}
On your account_settings.php page have this code:
if (isset($_GET['message'])) {
$message = $_GET['message'];
if ($message == "success") {
echo "Success";
} else {
echo "Error";
}
}
This removes the need of CONSTANT SESSION vars. and gives you plenty of flexibility.
header("Location: account_settings.php?message=No%20results%20found");
//%20 are URL spaces. I don't know if these are necessary.
If you need you can add more then one.
header("Location: account_settings.php?message=error&reason=No%20Results&timestamp=" . Date());
then account_settings.php can be:
if (isset($_GET['message'])) {
$message = $_GET['message'];
$reason = $_GET['reason'];
$time = $_GET['timestamp'];
if ($message == "success") {
echo "Success";
} else {
echo "Error: <br/>";
echo "Reason: $reason";
}
}
But remember GET exposes your messages in the browsers URL. So DON'T send sensitive information unless you secure it. Hope this helps.

Infinite redirect loop from PHP code in Wordpress

I have a PHP script which I will post below. It is a voting website, and my client only wants one user to be able to vote once based on their cookies and IP address.
After voting once, if the cookie or IP is detected as the same they are redirected to a fake voting pg which allows multiple votes. The browser loops between both the legal and duplicate vote pages.
Here is the code, I only added in the exit and die functions after getting this error and seeing online that might be the cause - however adding those functions made no difference.
$q = mysql_query("SELECT * FROM votelog");
while($row = mysql_fetch_array($q))
{
if(($ip = $_SERVER['REMOTE_ADDR']) == $row['ip'])
{
$duplicateIP = true;
}//end if
if(($row['pollid'] == 8))
{
$duplicatePoll = true;
}//end if
}//end while
//check cookies
if(isset($_COOKIE['poll']))
{
$cookieCheck = true;
}//end if
if((($duplicateIP == true) && ($duplicatePoll == true)) or ($cookieCheck == true))
{
show this pg
}//end if
else
{
echo '<meta http-equiv="refresh" content="0; url=/polls/legit" />'; //redirect to legal pg
exit();
die();
}//end else
Any ideas? The other page is the same except that the if and else are switched, like this:
if((($duplicateIP == true) && ($duplicatePoll == true)) or ($cookieCheck == true))
{
echo '<meta http-equiv="refresh" content="0; url=/polls/dupe" />'; //redirect to duplicate
exit();
die();
}//end if
else
{
show this pg
}//end else
P.S - I'm operating in a Wordpress environment
It is hard to guess whats going on there. But if your code is fine, i would expect that you have an caching issue.
Setting the headers (header()) correct, would help to solve that issue. But be carefull, you can make it more worse with setting wrong headers.
So an easy workaround could be to add an ?time() to your url. So the redirected URL would change each second.
echo '<meta http-equiv="refresh" content="0; url=/polls/dupe?'.time().'" />'; //redirect to duplicate
Just a side note:
exit();
die(); // this will never reached, as exit() and die() is the same
About exit() and die()

Header Location dont work on live server but works on localhost

I have this code which relocates a user to index.php if they set the value of a dropdownmenu
on an irrelevant page to set it for please check my code.
if(isset($_GET['d'])&&empty($_GET['d'])===false){
$cur_page=$_SERVER['PHP_SELF'];
$current_page = substr($cur_page,1);
$possible_page = array('terms.php','contact.php','about.php');
if(in_array($current_page,$possible_page)){
header('Location:/index.php?d='.$_GET['d'].'');
exit();
}else{
echo $_GET['d'];
}
It works fine on my localserver but on live server it does not ?
add ob_start(); at very beginning of the php script. If it include another file then do not use ?> in the end. Thanks
I always use this little method and works perfect in all situation.
public static function Redirect($sec, $file)
{
if (!headers_sent())
{
header( "refresh: $sec;url=$file" );
}
elseif (headers_sent())
{
echo '<noscript>';
echo '<meta http-equiv="refresh" content="'.$sec.';url='.$file.'" />';
echo '</noscript>';
}
else
{
echo '<script type="text/javascript">';
echo 'window.location.href="'.$file.'";';
echo '</script>';
}
}
In last case it will redirect for sure.

Categories