We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,322 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

require_once, function

I'm a noob, I know very little about php. Anyway, my page won't load at all, browser says its redirecting in a way that is not loading. As the title suggests I think the issue is with one of the php files I include with require_once(). Let me just show you:

thumbnail.php:

<?php
if(!function_exists("makethumbnail"))
{
    //die("right before function definition");
    function makethumbnail($src,$new_name,$new_width,$new_height)
    {
        die("inside makethumbnail");
        $si = imagecreatefromjpeg($src);
        $w = imagesx($si);
        $h = imagesy($si);
        $vi = imagecreatetruecolor($new_width,$new_height);
        imagecopyresampled($vi,$si,0,0,0,0,$new_width,$new_height,$w,$h);
        imagejpeg($vi,$new_name);
    }
}
?>

index.php:

<?php 
require_once("shopsite/thumbnail.php");
require_once("shopsite/checksession.php");
die("made it past the require_once");
$con = mysql_connect('localhost','jddancks','csc255');
mysql_select_db('test',$con);
$q = mysql_query("select prod_name,image_name,type1,type2 from Product",$con) or die("its the mysql");
$row = mysql_fetch_assoc($q);
$totalRows_Recordset1 = mysql_num_rows($q);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Welcome to the shopsite</title>
<script type="text/javascript" src="shopsite/lib/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="shopsite/lib/jquery.jcarousel.min.js"></script>
<link rel="stylesheet" type="text/css" href="shopsite/skins/ie7/skin.css" />
<script type="text/javascript">
JQuery(document).ready(function() {
    JQuery('#mycarousel').jcarousel({
        itemLoadCallback:itemLoadCallbackFunction,
        start:1,
        auto:3,
        animation:"slow",
        wrap:"both"
    });
});
</script>
</head>
<body>
<?php
session_start();
if(isset($_SESSION['nick']))
{
    echo "<p>Welcome, ".$_SESSION['nick']."</p>";
}
die("Made it past the first php");
?>
<h1>Welcome to the one-stop shop for your every need!</h1>
<div class="jcarousel-ie7">
<p>Browse Items:</p>
  <div class="jcarousel-container">
    <div class="jcarousel-clip">
      <ul id="mycarousel" class="jcarousel-skin-ie7">
      <?php
      $cnt=1;
      do
      {
            $i=$row['image_name'];
            $name=preg_split("/.jpg/",$i);
            $name = "shopsite/thumb/".$name[0]."-index.jpg";
            if(!file_exists($name))
            {
                makethumbnail("shopsite/images/".$row['image_name'],$name,50,50);
            }
            echo "         <li class=\"jcarousel-item-".$cnt."\"><img src=\"".$name."\" /></li>\n";
            $cnt=$cnt+1;
            if($cnt>12) die("cnt larger than 12");
       }
       while($row = mysql_fetch_assoc($q));
       ?>
       </ul>
     </div>
     <div disabled="disabled" class="jcarousel-prev jcarousel-prev-disabled"></div>
     <div class="jcarousel-next"></div>
   </div>
</div>
</body>
</html>
<?php mysql_free_result($row);
mysql_close($con); ?>

I want to insert images into a jquery carousel so a visitor can browse items they may want to purchase. I've never used jcarousel so I don't know if what I have works, I'm pretty sure thats not the problem. I guess its just one f those things you need a second pair of eyes for. The die statements in thumbnail.php make me believe that is the culprit, but it won't make it to the first line of the function, which is really confusing. I don't know how the php preporcessor works, other than its client-side.

2
Contributors
18
Replies
2 Days
Discussion Span
7 Months Ago
Last Updated
19
Views
Question
Answered
dancks
Junior Poster
144 posts since Nov 2010
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 0

remove line 4 from the index.php

Squidge
Posting Pro in Training
413 posts since Dec 2009
Reputation Points: 111
Solved Threads: 62
Skill Endorsements: 5

Ok I wan't being clear. I put die() in various places to help with debugging. It never made to line 4. It never made past line 2.

dancks
Junior Poster
144 posts since Nov 2010
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 0

Does this file exist: shopsite/thumbnail.php

Is it (from the file calling this), in the folder shopsite?

Squidge
Posting Pro in Training
413 posts since Dec 2009
Reputation Points: 111
Solved Threads: 62
Skill Endorsements: 5

yes to all of the above, and I know to some degree its being read because I see "right before function definition" when the die() isn't commented out.

dancks
Junior Poster
144 posts since Nov 2010
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 0

Ok, your not passing the required variables that you have set for the function:

if(!function_exists("makethumbnail"))
{
//die("right before function definition");
function makethumbnail($src,$new_name,$new_width,$new_height)
Squidge
Posting Pro in Training
413 posts since Dec 2009
Reputation Points: 111
Solved Threads: 62
Skill Endorsements: 5

I'm sorry I don't think I understand. do you mean:

    if(!function_exists("makethumbnail"))
    {
    $src=0;
    $new_name=0;
    $new_width=0;
    $new_height=0;
    //die("right before function definition");
    function makethumbnail($src,$new_name,$new_width,$new_height)

that seems nuts. If you mean later in my code:

$i=$row['image_name'];
$name=preg_split("/.jpg/",$i);
$name = "shopsite/thumb/".$name[0]."-index.jpg";
if(!file_exists($name))
{
makethumbnail("shopsite/images/".$row['image_name'],$name,50,50);
}

That really shouldn't matter, as that should result in some fatal error to be thrown when "createtrueimage" or another function is called. Not, "Page will not load". Right? I really need someone to break down the php compiler for me.

dancks
Junior Poster
144 posts since Nov 2010
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 0

wait a minute. Do you mean to tell me that when I include the file the function is actually executing? with null values?

dancks
Junior Poster
144 posts since Nov 2010
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 0

Shazaammm

remove this line

if(!function_exists("makethumbnail"))

Squidge
Posting Pro in Training
413 posts since Dec 2009
Reputation Points: 111
Solved Threads: 62
Skill Endorsements: 5

it didn't help

dancks
Junior Poster
144 posts since Nov 2010
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 0

That was a really crappy post. But basically the same thing happened. I'm curious as to why you thought that might be the problem. How exactly does php read and store a function?

dancks
Junior Poster
144 posts since Nov 2010
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 0

Because ur include is firing the function straight away. That line shouldnt be there. Have a looked on php.net on how functions work? I would suggest u have a look

Squidge
Posting Pro in Training
413 posts since Dec 2009
Reputation Points: 111
Solved Threads: 62
Skill Endorsements: 5

It still doesn't make any sense. Why should it differ from a function defined right in the file itself? if I cp'ed the function def where require_once is, would it work?

also, I'm trying a different approach with a class.

imageconvert.php:

<?php
class imageconvert
{
    die("inside class");
    function imageconvert()
    {
    }
    function makethumbnail($src,$new_name,$new_width,$new_height)
    {
        die("inside makethumbnail");
        $si = imagecreatefromjpeg($src);
        $w = imagesx($si);
        $h = imagesy($si);
        $vi = imagecreatetruecolor($new_width,$new_height);
        imagecopyresampled($vi,$si,0,0,0,0,$new_width,$new_height,$w,$h);
        imagejpeg($vi,$new_name);
    }
}
?>

and index.php:

<?php 
require_once("shopsite/imageconvert.php");
require_once("shopsite/checksession.php");
die("made it past the require_once");
$ic = new imageconvert();
$con = mysql_connect('localhost','jddancks','csc255');
mysql_select_db('test',$con);
$q = mysql_query("select prod_name,image_name,type1,type2 from Product",$con) or die("its the mysql");
$row = mysql_fetch_assoc($q);
$totalRows_Recordset1 = mysql_num_rows($q);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Welcome to the shopsite</title>
<script type="text/javascript" src="shopsite/lib/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="shopsite/lib/jquery.jcarousel.min.js"></script>
<link rel="stylesheet" type="text/css" href="shopsite/skins/ie7/skin.css" />
<script type="text/javascript">
JQuery(document).ready(function() {
    JQuery('#mycarousel').jcarousel({
        itemLoadCallback:itemLoadCallbackFunction,
        start:1,
        auto:3,
        animation:"slow",
        wrap:"both"
    });
});
</script>
</head>
<body>
<?php
session_start();
if(isset($_SESSION['nick']))
{
    echo "<p>Welcome, ".$_SESSION['nick']."</p>";
}
die("Made it past the first php");
?>
<h1>Welcome to the one-stop shop for your every need!</h1>
<div class="jcarousel-ie7">
<p>Browse Items:</p>
  <div class="jcarousel-container">
    <div class="jcarousel-clip">
      <ul id="mycarousel" class="jcarousel-skin-ie7">
      <?php
      $cnt=1;
      do
      {
            $i=$row['image_name'];
            $name=preg_split("/.jpg/",$i);
            $name = "shopsite/thumb/".$name[0]."-index.jpg";
            if(!file_exists($name))
            {
                $ic->makethumbnail("shopsite/images/".$row['image_name'],$name,50,50);
            }
            echo "         <li class=\"jcarousel-item-".$cnt."\"><img src=\"".$name."\" /></li>\n";
            $cnt=$cnt+1;
            if($cnt>12) die("cnt larger than 12");
       }
       while($row = mysql_fetch_assoc($q));
       ?>
       </ul>
     </div>
     <div disabled="disabled" class="jcarousel-prev jcarousel-prev-disabled"></div>
     <div class="jcarousel-next"></div>
   </div>
</div>
</body>
</html>
<?php mysql_free_result($row);
mysql_close($con); ?>

No dice.

dancks
Junior Poster
144 posts since Nov 2010
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 0

Also, if a mod comes across this thread, Can you please switch the tag with the title, I mixed them up when posting the thread. Thanks.

dancks
Junior Poster
144 posts since Nov 2010
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 0

It can't be the include. I just tried replacing both "require_once" with the actual code. Nothing changed. I have no idea where its failing.

<?php 
session_start();
$session_name = "forces";
$com=0;
if(!function_exists("logout"))
{
    function logout()
    {
        $_SESSION = array();
        session_destroy();
        header('Location:http://www.google.com');
    }
}
if(!isset($_SESSION['time']) || !isset($_SESSION['nick']))
{
    $com=2;
    logout();
}
else if($_SESSION['time'] < time())
{
    $com=3;
    logout();
}
class imageconvert
{
    function imageconvert()
    {
        die("constructor called successfully");
    }
    function makethumbnail($src,$new_name,$new_width,$new_height)
    {
        die("inside makethumbnail");
        $si = imagecreatefromjpeg($src);
        $w = imagesx($si);
        $h = imagesy($si);
        $vi = imagecreatetruecolor($new_width,$new_height);
        imagecopyresampled($vi,$si,0,0,0,0,$new_width,$new_height,$w,$h);
        imagejpeg($vi,$new_name);
    }
}
die("made it past the require_once");
$ic = new imageconvert();
$con = mysql_connect('localhost','jddancks','csc255');
mysql_select_db('test',$con);
$q = mysql_query("select prod_name,image_name,type1,type2 from Product",$con) or die("its the mysql");
$row = mysql_fetch_assoc($q);
$totalRows_Recordset1 = mysql_num_rows($q);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Welcome to the shopsite</title>
<script type="text/javascript" src="shopsite/lib/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="shopsite/lib/jquery.jcarousel.min.js"></script>
<link rel="stylesheet" type="text/css" href="shopsite/skins/ie7/skin.css" />
<script type="text/javascript">
JQuery(document).ready(function() {
    JQuery('#mycarousel').jcarousel({
        itemLoadCallback:itemLoadCallbackFunction,
        start:1,
        auto:3,
        animation:"slow",
        wrap:"both"
    });
});
</script>
</head>
<body>
<?php
session_start();
if(isset($_SESSION['nick']))
{
    echo "<p>Welcome, ".$_SESSION['nick']."</p>";
}
die("Made it past the first php");
?>
<h1>Welcome to the one-stop shop for your every need!</h1>
<div class="jcarousel-ie7">
<p>Browse Items:</p>
  <div class="jcarousel-container">
    <div class="jcarousel-clip">
      <ul id="mycarousel" class="jcarousel-skin-ie7">
      <?php
      $cnt=1;
      do
      {
            $i=$row['image_name'];
            $name=preg_split("/.jpg/",$i);
            $name = "shopsite/thumb/".$name[0]."-index.jpg";
            if(!file_exists($name))
            {
                $ic->makethumbnail("shopsite/images/".$row['image_name'],$name,50,50);
            }
            echo "         <li class=\"jcarousel-item-".$cnt."\"><img src=\"".$name."\" /></li>\n";
            $cnt=$cnt+1;
            if($cnt>12) die("cnt larger than 12");
       }
       while($row = mysql_fetch_assoc($q));
       ?>
       </ul>
     </div>
     <div disabled="disabled" class="jcarousel-prev jcarousel-prev-disabled"></div>
     <div class="jcarousel-next"></div>
   </div>
</div>
</body>
</html>
<?php mysql_free_result($row);
mysql_close($con); ?>
dancks
Junior Poster
144 posts since Nov 2010
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 0

Class and HTML should be seperate. Look here for a class on how to resize http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/

Squidge
Posting Pro in Training
413 posts since Dec 2009
Reputation Points: 111
Solved Threads: 62
Skill Endorsements: 5

Thats PHP 5. I'm working with 4.3.2 on the school server

dancks
Junior Poster
144 posts since Nov 2010
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 0

I got it to read imageconvert just fine now:

<?php
class imageconvert
{
    /*public function imageconvert()
    {
        die("constructor called successfully");
    }*/
    function makethumbnail($src,$new_name,$new_width,$new_height)
    {
        die("inside makethumbnail");
        $si = imagecreatefromjpeg($src);
        $w = imagesx($si);
        $h = imagesy($si);
        $vi = imagecreatetruecolor($new_width,$new_height);
        imagecopyresampled($vi,$si,0,0,0,0,$new_width,$new_height,$w,$h);
        imagejpeg($vi,$new_name);
    }
}
?>

and new main:

<?php 
include("shopsite/checksession.php");
die("made it past first include");
include("shopsite/imageconvert.php");
//die("made it past the require_once");
$ic = new imageconvert;
$con = mysql_connect('localhost','jddancks','csc255');
mysql_select_db('test',$con);
$q = mysql_query("select prod_name,image_name,type1,type2 from Product",$con) or die("its the mysql");
$row = mysql_fetch_assoc($q);
$totalRows_Recordset1 = mysql_num_rows($q);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Welcome to the shopsite</title>
<script type="text/javascript" src="shopsite/lib/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="shopsite/lib/jquery.jcarousel.min.js"></script>
<link rel="stylesheet" type="text/css" href="shopsite/skins/ie7/skin.css" />
<script type="text/javascript">
JQuery(document).ready(function() {
    JQuery('#mycarousel').jcarousel({
        itemLoadCallback:itemLoadCallbackFunction,
        start:1,
        auto:3,
        animation:"slow",
        wrap:"both"
    });
});
</script>
</head>
<body>
<?php
//session_start();
if(isset($_SESSION['nick']))
{
    echo "<p>Welcome, ".$_SESSION['nick']."</p>";
}
die("Made it past the first php");
?>
<h1>Welcome to the one-stop shop for your every need!</h1>
<div class="jcarousel-ie7">
<p>Browse Items:</p>
  <div class="jcarousel-container">
    <div class="jcarousel-clip">
      <ul id="mycarousel" class="jcarousel-skin-ie7">
      <?php
      $cnt=1;
      do
      {
            $i=$row['image_name'];
            $name=preg_split("/.jpg/",$i);
            $name = "shopsite/thumb/".$name[0]."-index.jpg";
            if(!file_exists($name))
            {
                $ic->makethumbnail("shopsite/images/".$row['image_name'],$name,50,50);
            }
            echo "         <li class=\"jcarousel-item-".$cnt."\"><img src=\"".$name."\" /></li>\n";
            $cnt=$cnt+1;
            if($cnt>12) die("cnt larger than 12");
       }
       while($row = mysql_fetch_assoc($q));
       ?>
       </ul>
     </div>
     <div disabled="disabled" class="jcarousel-prev jcarousel-prev-disabled"></div>
     <div class="jcarousel-next"></div>
   </div>
</div>
</body>
</html>
<?php mysql_free_result($row);
mysql_close($con); ?>

ok now I'm having trouble with checksession.php:

<?php
session_start();
$session_name = "forces";
$com=0;
function logout()
{
    $_SESSION = array();
    session_destroy();
    header('Location:http://cs4.sunyocc.edu/~j.d.dancks/index.php');
}
if(!isset($_SESSION['time']) || !isset($_SESSION['nick']))
{
    $com=2;
    logout();
}
else if($_SESSION['time'] < time())
{
    $com=3;
    logout();
}
//header(sprintf("Location:http://cs4.sunyocc.edu/~j.d.dancks/politicalforum/errorcheck.php?thing=%s",$com));
?>

I'm afraid to use a function here for scope reasons but I'm wondering if I should do away with it altogether just because of potential scope issues.

dancks
Junior Poster
144 posts since Nov 2010
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 0

Killed it. (Sort of. Now I have to figure out the image fixing nonsense) Fixed main:

<?php 
session_start();
$session_name = "forces";
$com=0;
function logout()
{
    $_SESSION = array();
    session_destroy();
    //header('Location:http://cs4.sunyocc.edu/~j.d.dancks/index.php');
}
if(!isset($_SESSION['time']) || !isset($_SESSION['nick']))
{
    $com=2;
    logout();
}
else if($_SESSION['time'] < time())
{
    $com=3;
    logout();
}
//die("made it past first include");
include("shopsite/imageconvert.php");
//die("made it past the require_once");
$ic = new imageconvert;
$con = mysql_connect('localhost','jddancks','csc255');
mysql_select_db('test',$con);
$q = mysql_query("select prod_name,image_name,type1,type2 from Product",$con) or die("its the mysql");
$row = mysql_fetch_assoc($q);
$totalRows_Recordset1 = mysql_num_rows($q);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Welcome to the shopsite</title>
<script type="text/javascript" src="shopsite/lib/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="shopsite/lib/jquery.jcarousel.min.js"></script>
<link rel="stylesheet" type="text/css" href="shopsite/skins/ie7/skin.css" />
<script type="text/javascript">
JQuery(document).ready(function() {
    JQuery('#mycarousel').jcarousel({
        itemLoadCallback:itemLoadCallbackFunction,
        start:1,
        auto:3,
        animation:"slow",
        wrap:"both"
    });
});
</script>
</head>
<body>
<?php
//session_start();
if(isset($_SESSION['nick']))
{
    echo "<p>Welcome, ".$_SESSION['nick']."</p>";
}
//die("Made it past the first php");
?>
<h1>Welcome to the one-stop shop for your every need!</h1>
<div class="jcarousel-ie7">
<p>Browse Items:</p>
  <div class="jcarousel-container">
    <div class="jcarousel-clip">
      <ul id="mycarousel" class="jcarousel-skin-ie7">
      <?php
      $cnt=1;
      do
      {
            $i=$row['image_name'];
            $name=preg_split("/.jpg/",$i);
            $name = "shopsite/thumb/".$name[0]."-index.jpg";
            if(!file_exists($name))
            {
                $ic->makethumbnail("shopsite/images/".$row['image_name'],$name,50,50);
            }
            echo "         <li class=\"jcarousel-item-".$cnt."\"><img src=\"".$name."\" /></li>\n";
            $cnt=$cnt+1;
            if($cnt>12) die("cnt larger than 12");
       }
       while($row = mysql_fetch_assoc($q));
       ?>
       </ul>
     </div>
     <div disabled="disabled" class="jcarousel-prev jcarousel-prev-disabled"></div>
     <div class="jcarousel-next"></div>
   </div>
</div>
</body>
</html>
<?php mysql_free_result($row);
mysql_close($con); ?>
dancks
Junior Poster
144 posts since Nov 2010
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 7 Months Ago by Squidge

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1368 seconds using 2.9MB