I have this php code in which i attempted to store a value from three select input into a one variable. it is not working. what is the right way? or is that possible?
$REGDATE = mysql_real_escape_string ($_POST["month"]) "/" ($_POST["day"]) "/" ($_POST["year"]);
What you're looking for is to concatenate the strings
mysql_real_escape_string(
$_POST["month"] . "/" .
$_POST["day"] . "/" .
$_POST["year"] )
As mentioned there are better ways of doing this, but for what you're doing you need to join the strings together.
Related
Ok, so right now I've got this URL, which works perfectly:
$html_string = file_get_contents('https://www.123.com/' . $_GET["ticket"]);
What I want to do is to insert $_GET between the URL.
Here's what I mean:
('https://www.123.com/' . $_GET["ticket"] /url-continues-here);
I have tried everything, but can't find any solution how to do it without an error.
Concatenate the 2 strings first and after that use file_get_contents()
$html_string = 'https://www.123.com/' . $_GET["ticket"] . '/url-continues-here';
file_get_content($html_string);
I have three HTML form field values (name, saywords, mail) that I try to concat in php and write into one single txt file on my server:
Each value should be on a new line in the txt field, as is why I added the "\n" in the array .... where's my error?
Thanks a lot for any help!
$name = $_POST['name'];
$saywords = $_POST['saywords'];
$mail = $_POST['mail'];
$data = array($name, . "\n" $mail, . "\n" $saywords);
file_put_contents("$t.$ip.txt",$data); // Will put the text to file
You have two problems:
$data should be a string not an array
. concatenates the left hand side and the right hand side: "abc" . "def" becomes "abcdef".
putting the dot first like in . "\n" or even . "\n" $mail doesn't make sense in PHP so you'll get a parse error.
Replace your $data = line with $data = $name . "\n" . $mail . "\n" . $saywords; and you'll be good to go.
i don't see the use of array , you can concatenate them just like :
$data = $name . "\n" . $mail . "\n" . $saywords ;
It depends with the operating system of the server.
Try "\r\n" instead of "\n"
Okay, so here is my shot.
/*first of all, you should always check if posted vars are actually set
and not empty. for that, you can use an universal function "empty", which
checks if the variable is set / not null / not an empty string / not 0.
In such way you will avoid PHP warnings, when some of these variables
will not be set*/
$name = !empty($_POST['name']) ? $_POST['name'] : '';
$saywords = !empty($_POST['saywords']) ? : $_POST['saywords'] : '';;
$mail = !empty($_POST['mail']) ? $_POST['mail'] : '';
/*Secondly, do not use \n, \r, \r\n, because these are platform specific.
Use PHP_EOL constant, it will do the job perfectly, by choosing
what type of line-break to use best.
As others mentioned - in your scenario, the string would be better solution.
Add everything into string, and then put its contents into file. Avoid using
double quotes, when you define PHP strings, and use single quotes instead - for
performance and cleaner code.
*/
$data = 'Name: '.$name.PHP_EOL.'E-Mail: '.$mail.PHP_EOL.'Message: '.$saywords.PHP_EOL.PHP_EOL;
file_put_contents($t.$ip.'.txt', $data); // Will put the text to file
By the way, I strongly suggest to also add some extra validation, before saving data to that txt file. With this code somebody can easily mess up contents of your txt file, by posting huge amounts of data with no limits.
Tips:
1) Accept only Names with limited lengths and charaters (do not allow to use special symbols or line breaks - you can also filter them out, before saving)
2) Validate e-mail which has been entered - if it is in correct format, does mx records exists for the domain of e-mail address, and so on...
3) Accept "saywords" with limited length, and if needed - deny or filter out special characters.
You will get much cleaner submissions by doing this way.
Use <br /> as you use html code
I have been finding myself doing URLs like this:
$link = base_url('post') . '/' . $post_id . '/' . $slug . '/page/' . $page_num;
To form http://example.com/post/10/some-post-name/page/1
Needless to say, it's pretty messy, but I can't think of an alternative? Is there a better way write links with variables in it?
I am using Codeigniter as a framework if there is a solution involving it.
You have a few ways:
First, via sprintf:
sprintf('%s/%s/%s/page/%s', base_url('post'), $post_id, $slug, $page_num);
Or via an array implode:
implode('/', array(base_url('post'), $post_id, $slug, 'page', $page_num));
Or if you put all your values into variables, you can take advantage of string interpolation.
$url = ...;
...
"$url/$post_id/$slug/page/$page_num";
The last one is longer when you take into account the variable assignment block, but it combines succintness with readability.
Use sprintf:
$link = sprintf('%s/%d/%s/page/%d', base_url('post'), $post_id, $slug, $page_num);
You could do something like this:
$link = site_url("post/{$post_id}/{$slug}/page/{$page_num}");
You really should be using site_url() instead of base_url() for CI links. base_url() is meant for non-CI assets, like images and css.
site_url() will point to the correct front controller path, so you can update your configuration at will, and everything using that to build paths will update accordingly.
I revised my answer. Use the curly brace notation and avoid using extra functions. You can pass an array of arguments to the function, like so:
$link = site_url(array('post', $post_id, $slug, 'page', $page_num));
But working with arrays is slower. This can be useful if you need to dynamically build the url, though.
You could do it the old fashioned way with a function!
function buildlink($base_url,$post_id,$slug,$page_num)
{
return $base_url . '/' . $post_id . '/' . $slug . '/page/' . $page_num;
}
call it like this
$link = buildlink(base_url('post') ,$post_id, $slug ,$page_num);
but maybe i'm missing something
I have to define two variables:
<?php
$path = 'http://' . $_SERVER['SERVER_NAME'] . '/root/images/folderX/';
$files = scandir('images/folderX/');
?>
In the place of 'folderX' I should use a dynamic value, which comes from a query, like
<?php echo $row_rsQuery["item_name"];?>
How can it be done?
I'm not too familiar with php, and I will perhaps never learn it (..too old..), but I solve most of my problems with Dreamweaver, however the above problem is beyond its (and my) capabilities...
String concatenation (appending one string to another) is done via the . operator:
$files = scandir('images/'.$row_rsQuery["item_name"]);
$path = 'http://' . $_SERVER['SERVER_NAME'] . '/root/images/'.$row_rsQuery["item_name"].'/';
$files = scandir('images/'.$row_rsQuery["item_name"].'/');
I'm using the following code but it gives me an error about an unexpeted " in the statement at this line, but I'm not sure how the syntax should go:
list($width,$height,$type,$attr) = getimagesize("' . $SESSION_["html_folder"] . '/uploadedfiles/' . $row['logo'] . '");
echo "<p>This logo is ".$width; x $.height; echo "pixels in size.</p>";
PHP says the error is on that first line.
Try this
list($width,$height,$type,$attr) = getimagesize($SESSION_['html_folder'] . '/uploadedfiles/' . $row['logo']);
The error in your code was that you opened the string "twice", one with " and one with '. If you use a variable as parameter where a string is expected, you do not need to set the variable in quotes.
You should make sure that in $SESSION_['html_folder'] there's no malicious code, e.g. with
if(!is_dir($SESSION_['html_folder']))
die("ERROR");
youre adding all kinds of unnecessary quotes
list($width,$height,$type,$attr) = getimagesize($SESSION_["html_folder"] . '/uploadedfiles/' . $row['logo']);
You want:
list($width,$height,$type,$attr) = getimagesize($_SESSION["html_folder"] . '/uploadedfiles/' . $row['logo']);
The main issue was that you were using double quotes to open a string, but you didn't close the string with double quotes. The above is a better formated string(and fixed your $_SESSION variable).