Jump to content

Keep a $variable constant in file


infrabyte

Recommended Posts

Hi All,

 

I have a file that passes on a mobile number eg 0123456789 in the url as follows http://domain.com/image.php?mobile=0824929698

in my file image.php I use the $mobile = $_GET['mobile'];

this works great if I echo the result and it works.

My problem is I want to use the $mobile further down in my script to write a filename using the number in my images folder. For some reason it just looses the value of $mobile when I want to use it. Can someone please help me to understand why it disappears.

Thank you in advance.

here is my code...

<?php

$mobile = $_GET['mobile'];
define ("MAX_SIZE","2048"); 

//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}

$errors=0;
//checks if the form has been submitted
if(isset($_POST['Submit'])) 
{
	$image=$_FILES['image']['name'];
	if ($image) 
	{
	//get the original name of the file from the clients machine
		$filename = stripslashes($_FILES['image']['name']);
		$extension = getExtension($filename);
		$extension = strtolower($extension);

if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
		{
	//print error message
			echo '<h1>Unknown extension!</h1>';
			$errors=1;
		}
		else
		{
//get the size of the image in bytes
$size=filesize($_FILES['image']['tmp_name']);

if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}

$image_name=$mobile.'.'.$extension;
$newname="images/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) 
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}

if(isset($_POST['Submit']) && !$errors) 
{
echo $mobile;
	echo "<h1>Thanks for entering the Search. Good Luck.</h1>";
}
?>

<form name="newad" method="post" enctype="multipart/form-data" action="image.php">
<table>
<tr><td><b>Please upload a head and shoulders photo of yourself - Maximum size: 2Mb Jpeg </b><input type="file" name="image"></td></tr>
<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>
</table>	
</form>

 

The problem area is here...$image_name=$mobile.'.'.$extension;

 

Link to comment
https://forums.phpfreaks.com/topic/241788-keep-a-variable-constant-in-file/
Share on other sites

infrabyte,

 

Your $_GET parm is empty because you are submitting your form with post method.

 

If you "got" your mobile parm from another page with method="GET" you can save the variable for later use by learning and implementing $_SESSION variables.

 

( In a nut shell, at the top of each script enter: session_start();  Then, once your $_GET['mobile'] parm is set to $mobile, do: $_SESSION['mobile'] = $mobile;

 

Later when you want this value on another page, start the page with session_start(); then code: $mobile = $_SESSION['mobile'] and you should be good to go. )

 

Scot L. Diddle, Richmond VA

 

Thank you for your help.

 

After looking up a bit on session_start(); on php.net and what you said, I got it working.

I just put session_start(); at the top of each page and called

$mobile = $_SESSION['mobile']; on the second one

Works like a charm.

 

Thank you

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.