im trying to check two variables in a url, U and TAG. i keep getting whitescreen or no results. i want to check U and the preg replace works and everything, but! i also want to check the variable TAG to make sure it isnt blank, and if it is blank then go to website.com
here is a url
http://www.website.com/hashtag.php?u=frank&tag=#my
here is the code
if(isset($_GET["u"]))
{
$u = preg_replace('#[^a-z0-9]#i', '', $_GET['u']);
} else {
header("location: http://www.website.com");
exit();
}
if (isset($_GET['tag']) && $tag == "")
{
header("location: http://www.website.com");
exit();
}
f (isset($_GET['tag']) && $tag == "")
-> $tag isn't an existing var for now, if you want to call $tag you should previously do :
$tag = $_GET['tag'];
Related
I have page with 4 links, when someone clicks on one of the links I want to set a cookie to store the selected link, then when they return to the site, the cookie redirects them to the link they previously selected. Since I only want the 4 links to set a cookie I'm using a query string (?sel=p1) in the link and checking for that to set the cookie.
function set_pref_cookie(){
if (isset($_GET['sel'])) {
$root = $_GET['sel'];
if ($root = 'p1'){
$cookie_var = '/page1/';
} else if ($root = 'p2'){
$cookie_var = '/page2/';
} else if ($root = 'p3'){
$cookie_var = '/page3/';
} else if ($root = 'p4'){
$cookie_var = '/page4/';
}
} else {
$root = '';
}
if ($root !=''){
setcookie('pref_sel',$_COOKIE['sel'] = $cookie_var, time()+60*60*24*5, "/");
}
if (isset($_COOKIE['pref_sel']) && $_COOKIE['pref_sel'] != ''){
header('Location:' . $_COOKIE['pref_sel']);
exit;
}
}
add_action('init','set_pref_cookie');
The issue is, all 4 links set the same value in the cookie /page1/
, and, on return to the site, I'm getting a redirect loop.
I've also tried checking for an empty cookie
if (isset($_COOKIE['pref_sel']) && !empty($_COOKIE['pref_sel']) ){
but same result.
To refer to all the comments in your first ticket, and this one (please don't use 2 questions for the same problem)
=> Redirection with cookie without loop : ok fixed by the correction i gave you and explained to you in the first question.
=> second problem : you're always redirected to page-1.
=> please fix this part of you're code like this :
if ($root == 'p1'){
$cookie_var = '/page1/';
} else if ($root == 'p2'){
$cookie_var = '/page2/';
} else if ($root == 'p3'){
$cookie_var = '/page3/';
} else if ($root == 'p4'){
$cookie_var = '/page4/';
}
You also should add an else case cause your $cookie_var could have sometimes empty value.
=> third, what do you want to achieve with your setcookie with an affectation ?
setcookie('pref_sel',$_COOKIE['sel'] = $cookie_var, time()+60*60*24*5, "/");
you should only do this :
setcookie('pref_sel', $cookie_var, time()+60*60*24*5, "/");
=> to debug, comment you're redirection and replace it by
echo $_COOKIE['pref_sel'];
First thing first, i'm new to php.
I'm trying to check if two variables are both empty, and if so merge them togheter to display only one result, but if one of them is not empty, than i have to display is value.
I currently have:
$customerNotesWC = "";
$DYSPrintableOrderNotes = "test note";
Here's the code i tried so far:
function displayCustomerOrderNotes($customerNotesWC,$DYSPrintableOrderNotes){
if ($customerNotesWC == "") {
$customerNotesWC = "(none)";
}
if ($DYSPrintableOrderNotes ==""){
$DYSPrintableOrderNotes = "(none)";
}
if ($DYSPrintableOrderNotes == "(none)" && $customerNotesWC == "(none)"){
$NotesToDisplay = "(none)";
}
else {
$NotesToDisplay = $customerNotesWC . "<br/>" . $DYSPrintableOrderNotes;
}
}
Unfortunately this doesn't work, as the results is the following:
(none)
Pre-sale order note
I know that there must be a better way to achieve this, and any suggestions will be really appreciated.
Thanks a lot
In order to get your desired functionality of only printing "(none)" when both values are blank, the simplest thing to do to fix your code is remove the code that's setting either value to "(none)" and change your main if-statement to check for "":
function displayCustomerOrderNotes($customerNotesWC,$DYSPrintableOrderNotes){
if ($DYSPrintableOrderNotes == "" && $customerNotesWC == ""){
$NotesToDisplay = "(none)";
}
else {
$NotesToDisplay = $customerNotesWC . "<br/>" . $DYSPrintableOrderNotes;
}
//shouldn't there be an echo $NotesToDisplay; or return $NotesToDisplay; ? or something?
}
I am struggling to redirect the user if the cookie does not equal a vairable. If it does equal the vairable, then it should continue the script. Here is my code to redirect :
if(empty($_GET)) {
//No variables are specified in the URL.
//Do stuff accordingly
echo "No variables specified in URL...";
} else {
//Variables are present. Do stuff:
$id = htmlspecialchars($_GET["id"]);
echo 'url query is ' . $id;
}
if(isset($_COOKIE['logged_in']) == $id)
{
header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) != $id)
{
//continues the script
Please note that the vairable in the if statment ($id) is a vairable from the query of url; for example if the url is, "random.com/test.php?id=17" and the cookie equals 18 the script should redirect. However if url is, "random.com/test.php?id=17" and the cookie equals 17, then stay on the same page. Sorry if it sounds complecated.
It doesnt work as this code: It doesnt redirect no matter what the vairable equals. Thanks
Are you looking for something like this. If so, it should work for your case:
<?php
if(empty($_GET)) {
//No variables are specified in the URL.
//Do stuff accordingly
echo "No variables specified in URL...";
} else {
//Variables are present. Do stuff:
$id = htmlspecialchars($_GET["id"]);
echo 'url query is ' . $id;
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']==$id)
{
header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']!=$id)
{
//continues the script
}
?>
A headers will apply only after it send to client. If you want immediately redirect, you can put exit(0) after header(...) in this case you are stop executing of the script and will send current headers to the browser which will redirect you.
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']==$id) {
header("Location: test.php");
exit(0);
}
//continues the script
The problem is that you are comparing the "value" of isset (the result) with the value of your GET parameter, $id:
if(isset($_COOKIE['logged_in']) == $id)
What this says is "determine if $_COOKIE['logged_in'] is set and compare that determination to $id". PHP will evaluate isset, which returns true or false (as it says in the documentation), and compare that true or false to the other side of the expression (==), meaning $id, which will never match given your examples. If you query "random.com/test.php?id=true" (or false) that might do what you are looking for.
The line you have does not mean "determine if $_COOKIE['logged_in'] is set and compare the value of $_COOKIE['logged_in'] to the value of $id", which I believe is what you are looking for. In that case, what you want to do is first check that $_COOKIE['logged_in'] is set and then check that the value of $_COOKIE['logged_in'] matches $id, like so:
if (isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] == $id)
If that doesn't make sense, here is a really explicit version that might be clearer as to what is actually going on:
if ((isset($_COOKIE['logged_in']) == true) && ($_COOKIE['logged_in'] == $id))
Hope that helps.
you should add another condition.
if(empty($_GET)) {
//No variables are specified in the URL.
//Do stuff accordingly
echo "No variables specified in URL...";
} else {
//Variables are present. Do stuff:
$id = htmlspecialchars($_GET["id"]);
echo 'url query is ' . $id;
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] == $id)
{
header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] != $id)
{
//continues the script
or use this script
if(isset($_COOKIE['logged_in']))
{
if($_COOKIE['logged_in']==$id){
header("Location: test.php");
}
else{
//another condition to equal is not equal so directly we can use else
//continues the script
}
} else {
echo "Cookie not valid or available";
// redirect user
}
Having one of those brain fade moments this morning. I have the following php:
$imgset = $result->fields[6];
if ($imgset = '')
{
$imgset = 'logo';
}
else
{
$imgset = $result->fields[6];
}
echo '<img id="imgdisp" src="/img/'.$imgset.'.jpg" />';
$imgset = '';
Where it looks to see if the result from the database is blank and if so, should put in logo.jpg instead of whatever the result is. For some reason though, it just does not want to work and I am probably being completely blind, but cannot see why not. I still get blank images in the HTML and filenames of "/img/.jpg" as though $imgset is still passing through a blank. The values are not NULL in the SQL either, they are most definitely blank entries inputted from an inputbox using a _POST in a form elsewhere.
This:
if ($imgset = '') {
Is always setting $imgset to empty. Use comparison instead:
if ($imgset == '') {
Your else is also not needed since in that case $imgset is already set as $result->fields[6];.
Try to verify if the image exists in your path as well
<?php
$imgset = $result->fields[6];
if ($imgset) {
$imgset = $result->fields[6];
$path ='pathtoimages';
if(!file_exists($path.'/'.$imageset.'.jpg'){
$imgset = 'logo';
}
}
else
{
$imgset = 'logo';
}
echo '<img id="imgdisp" src="/img/'.$imgset.'.jpg" />';
?>
You forgot to compare on the if condition and instead you are assigning an empty value to $imgset. if ($imgset = '') should be if ($imgset == '')
$imgset = $result->fields[6];
if ($imgset == '')
{
$imgset = 'logo';
}
echo '<img id="imgdisp" src="/img/'.$imgset.'.jpg" />';
$imgset = '';
you do not need the else part as the value is already assigned in the first statement.
Using a ternary operator it can be done like this:
echo '<img id="imgdisp" src="/img/'.(empty($imgset)?'logo':$imgset).'.jpg" />';
Shorter code at the cost of readability.
This is the reason why it's better to reverse the condition:
if ('' = $imgset)
would have lead to an error.
The answer:
if ('' == $imgset)
//or
if (empty($imgset))
If you are selecting from MYSQL, you can use something like
SELECT *,COALESCE(image,"logo") AS image FROM ....
This way when the results come back and some rows have a NULL image, it will be replaced by "logo" so you don't need the IF logic in your PHP :)
If the variable below consists of no characters, just any number of spaces, I would like to redirect the user to a URL using header("Location: URL"); exit();.
How can I do that?
$comment = mysql_real_escape_string($_POST['comment']);
Here are 2 ways:
if (trim($var, ' ') == '') {
// $var consists of only spaces
}
// or
if (str_replace(' ', '', $var) == '') {
// $var consists of only spaces
}
I'd use the \s identifier for PCRE, this will also catch tabs and such:
if (preg_match('/^\s+$/', $comment) { ... }
[Edit] Or, if you want to also catch totally empty strings:
if (preg_match('/^\s*$/', $comment) { ... }
if(strlen(trim($comment)) == 0) header('Location: URL');
!trim( $_POST['comment'] ) && header( "Location: URL" );
if(str_replace(" ", "", $comment) == "")
{
header("Location: URL");
exit();
}