I am new to php and I have a problem with the following code:
$ID = $_POST["first_name"]
$EXT = ".html"
$DOMAIN = "blabla.com/membersarea/"
$URL = ($DOMAIN . $ID . $EXT)
header("location: http://".$URL);
Here is the error I'm getting:
Parse error: syntax error, unexpected T_VARIABLE
The error is on line 3:
$EXT = ".html"
So my question is: is the error because of a point in a php variable?
You missed semicolon ; in your code. Each statements should ends with semi-colon ;
<?php
$ID = $_POST["first_name"];
$EXT = ".html";
$DOMAIN = "blabla.com/membersarea/";
$URL = ($DOMAIN . $ID . $EXT);
header("location: http://".$URL);
?>
You need to use ; semi-colon delimiter to say php that this is the end of this line...
<?php
$ID = $_POST["first_name"];
$EXT = ".html";
$DOMAIN = "blabla.com/membersarea/";
$URL = ($DOMAIN . $ID . $EXT);
header("location: http://".$URL);
?>
Also use exit; after header()
<?php
$ID = $_POST["first_name"]; /* Sanitize your data, atleast use mysqli_real_escape_string()*/
$EXT = ".html";
$DOMAIN = "blabla.com/membersarea/";
$URL = ($DOMAIN.$ID.$EXT); /* Also don't leave any spaces here */
header("location: http://".$URL);
exit;
?>
you have to put semi-colon at the end of each line to tell php its the end of line and you are going to start next one.
So, in your code put semi-colon(;) in the first four lines.
Related
what's wrong?! line 16 is: header('Location: '.$get_link.'');
<?php
require_once("../libs/proxy.php");
require_once("../libs/config.php");
require_once("../libs/ip.php");
$rnd = intval(rand(1000,9999)); //generate random number
$lnk_lnk = "../$db_name/lnk/$userip.xxx"; //link path
$api_url = "http://btc.ms/?api=$btcms_api&url=$site_url/links/verifier.php?
id=$rnd&format=text";
$get_link = file_get_contents($api_url);
if (!empty($get_link)) {
$m_lnk = fopen($lnk_lnk, "w"); //open link file
fwrite($m_lnk, $rnd); //store random value
fclose($m_lnk);
header('Location: '.$get_link.''); //here is line 16!
} else {
header("Location: ../index.php?lnkmsg=Unknown Error.");
}
?>
thanks for help!
.
.
.
.
.
.
.
.
.
.
.
Maybe your URL contain multiple lines. You can use the function urlencode() to correct this error.
Try this,
header('Location: '.urlencode($get_link)); //here is line 16!
I want to write PHP code that check few conditions and then trigger JavaScript on the loaded page.
The php file is:
$jwplayer= "<script>jwplayer('video1').setup({playlist:$file});</script>";
$url2= "http://$_SERVER[HTTP_HOST]/index.php";
$url3= "http://$_SERVER[HTTP_HOST]/index3.php";
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if($url2==$url) {
$file= "/media/video.xml";
echo $jwplayer;
}
if($url3==$url) {
$file= "/media/video2.xml";
echo $jwplayer;
}
I'm using PHP include to include the above code.
If the URL of the page is equal to the value of $url2 above, then I want the playlist updated. This would be done by setting $file to "/media/video.xml" and executing the required JavaScript I am attempting to include.
Try this:
$file= "/media/default.xml";
$url = "http://" . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
$url2= "http://$_SERVER[HTTP_HOST]/index.php";
if($url2==$url) {
$file= "/media/video.xml";
}
$jwplayer= "<script>jwplayer(\"video1\").setup({playlist:".$file."});</script>";
echo $jwplayer;
I would start with fixing your syntax errors and go from there:
function jwPlayer($xml) {
echo("<script>jwplayer('video1').setup({playlist:'$xml'});</script>");
}
$url2= "http://" . $_SERVER["HTTP_HOST"] . "/index.php";
$url = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
if($url2 == $url) {
$file= "/media/video.xml";
jwPlayer($file);
} else {
jwPlayer("path/to/other/file.xml");
}
Defining a variable after its use will do nothing but cause you problems.
maybe like this
$file = "somefile.xml";
$url2= "http://$_SERVER[HTTP_HOST]/index.php";
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if($url2==$url)
$file= "/media/video.xml";
$jwplayer= "<script>jwplayer(\"video1\").setup({playlist:$file});</script>";
echo $jwplayer;
Try this..
$url2= "http://$_SERVER[HTTP_HOST]/index.php";
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if($url2==$url) {
$file= "/media/video.xml";
echo $jwplayer= "<script>jwplayer("video1").setup({playlist:$file});</script>";
}
in you code it looks like your trying to get the value of $file by echo'ing the script..
hope that helps
There is a few mistakes within this code.
In the moment you assign this string, the variable $file is undefined, you have to define it first.
$jwplayer= "<script>jwplayer('video1').setup({playlist:$file});</script>"
On a javascript object literal, you must have to quote a string. If you don't the content of the variable $file will become a undefined javascript variable.
{playlist:"$file"} //playlist is a string
Try this
$url2 = "http://$_SERVER[HTTP_HOST]/index.php";
$url = "http://" . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
if($url2==$url) {
$file= "/media/video.xml";
$jwplayer= "<script>jwplayer('video1').setup({playlist:'$file'});</script>";
echo $jwplayer;
}
I'm not sure what do you mean by "constant", but I would do something like this, but I can't tell for sure if this works just with the little information provided in the actual question.
<?php
$url2 = "http://$_SERVER[HTTP_HOST]/index.php";
$url = "http://" . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
$file = "";
if($url2==$url) {
$file = "/media/video.xml";
}
?>
<script>
jwplayer('video1').setup({playlist:'<?=$file>'});
</script>
I've been trying to create a directory following a specific structure, yet nothing appears to be happening. I've approached this by defining multiple variables as follows:
$rid = '/appicons/';
$sid = '$artistid';
$ssid = '$appid';
$s = '/';
and the function I've been using runs thusly:
$directory = $appid;
if (!is_dir ($directory))
{
mkdir($directory);
}
That works. However, I want to have the following structure in created directories: /appicons/$artistid/$appid/
yet nothing really seems to work. I understand that if I were to add more variables to $directory then I'd have to use quotes around them and concatenate them (which gets confusing).
Does anyone have any solutions?
$directory = "/appicons/$artistid/$appid/";
if (!is_dir ($directory))
{
//file mode
$mode = 0777;
//the third parameter set to true allows the creation of
//nested directories specified in the pathname.
mkdir($directory, $mode, true);
}
This should do what you want:
$rid = '/appicons/';
$sid = $artistid;
$ssid = $appid;
$s = '/';
$directory = $rid . $artistid . '/' . $appid . $s;
if (!is_dir ($directory)) {
mkdir($directory);
}
The reason your current code doesn't work is due to the fact you're trying to use a variable inside a string literal. A string literal in PHP is a string enclosed in single quotes ('). Every character in this string is treated as just a character, so any variables will just be parsed as text. Unquoting the variables so your declarations look like the following fixes your issue:
$rid = '/appicons/';
$sid = $artistid;
$ssid = $appid;
$s = '/';
This next line concatenates (joins) your variables together into a path:
$directory = $rid . $artistid . '/' . $appid . $s;
Concatenation works like this
$directory = $rid.$artistid."/".$appid."/"
When you're assigning one variable to another, you don't need the quotes around it, so the following should be what you're looking for.
$rid = 'appicons';
$sid = $artistid;
$ssid = $appid;
and then...
$dir = '/' . $rid . '/' . $sid . '/' . $ssid . '/';
if (!is_dir($dir)) {
mkdir($dir);
}
If I have filename.jpg, with PHP how would change it too filename123456789.jpg, where 123456789 is a timestamp, I currently do this,
$name = $filename;
$parts = explode(".", $name);
$filename = $parts[0].time().'.'.$parts[1];
however this just just leaves me with 123456789.
Your approach works fine too, but breaks if the filename has multiple dots. I'd rather use the pathinfo() function to accomplish this:
$info = pathinfo($filename);
$filename = $info['filename'].time().'.'.$info['extension'];
debug (output) your input and steps to find the error
function debug($var, $label = '') {
echo $label
. '<pre>'
. print_r($var, true)
. '</pre>';
}
$name = 'filename.bug.test.jpg';
debug($name, 'old filename');
$parts = explode('.', $name);
debug($parts, 'filenameparts');
$ext = array_pop($parts);
$prefix = implode('.', $parts);
$newName = $prefix . time() . '.' . $ext;
debug($newName, 'new filename');
as mention above use pathinfo instead of explode
i've used explode, couse i've used a dummy filename.
thats a no-brainer:
function getFileName($filename){
preg_match('#([^/]+)\.([\w]+)$#',$filename,$match);
return $match[1].time().'.'.$match[2];
}
I currently have:
<?php
if (isset($_POST["submitwrite"])) {
$handle = fopen("writetest.txt","w+");
if ($handle) {
fwrite($handle, "Dan"."¬".$_POST["username"]."¬".$_POST["pollname"]."¬".$_POST["ans1"]."¬".$_POST["ans2"]."¬".$_POST["ans3"]."¬".time());
fclose($handle);
}
}
?>
However I need to adjust the filename to be dynamic, instead of 'writetest.txt' I would like it to be: username+pollname+time.txt taking the $_post variables.
I would also like to change the directory these files are stored in to /results.
Help please...
You mean doing something like this?
$filename = '/results/' . $_POST['username'] . '/' . $_POST['pollname'] . '/time.txt';
if (isset($_POST["submitwrite"])) {
$handle = fopen($filename,"w+");
// etc...
Or am I not understanding you?
Edit
To address the issue BalusC pointed out, this is a more complete solution.
It makes sure the $_POST['username'] and $_POST['pollname'] values are valid, so they won't create an invalid or possibly harmful $filename.
<?php
$basedir = '/results';
$basename = 'time.txt';
// Get user and poll names
$username = $_POST['username'];
$pollname = $_POST['pollname'];
// Counteract the old magic_qutoes feature, if needed.
if(get_magic_quotes_gpc()) {
$username = stripslashes($username);
$pollname = stripslashes($pollname);
}
// Validate user and poll names.
$regexp = '/^[\w\d\_\-\. \']+$/iu';
if(!preg_match($regexp, $username) || !preg_match($regexp, $pollname)) {
echo 'Username or pollname is invalid. Aborting!';
}
else {
// Compile the complete file name
$filename = $basedir . '/' . $username . '/' . $pollname . '/' . $basename;
// Write to the file
if (isset($_POST["submitwrite"])) {
$handle = fopen($filename,"w+");
if ($handle) {
fwrite($handle, "Dan"."¬".$_POST["username"]."¬".$_POST["pollname"]."¬".$_POST["ans1"]."¬".$_POST["ans2"]."¬".$_POST["ans3"]."¬".time());
fclose($handle);
}
}
}
?>
fopen creates (at least tries) the file if it does not exist, so
$filename = $username . $pollname . $time . '.txt';
$handle = fopen($filename, 'w+');
will work fine.
By the way, w+ places the pointer at the beginning of the file. If the file already has some data, it will truncate it first. If you want to append data to the file, you may want to use 'a+'